0

コードを変更しようとしていますが、タイトルに記載されているエラーが発生しました。誰か助けてください。

エラー: BruteCollinearPoints 型のセグメント () メソッドは、引数 (ポイント、ポイント) には適用できません。

import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import edu.princeton.cs.algs4.*;

public class BruteCollinearPoints 
{

   public BruteCollinearPoints(Point[] points)    // finds all line segments containing 4 points
   {
       int N = points.length;
       int i = 0;

       for (int p = 0; p < N; p++) {
            for (int q = p + 1; q < N; q++) {
                double slopeToQ = points[p].slopeTo(points[q]);
                for (int r = q + 1; r < N; r++) {
                    double slopeToR = points[p].slopeTo(points[r]);
                    if (slopeToQ == slopeToR) {
                        for (int s = r + 1; s < N; s++) {
                            double slopeToS = points[p].slopeTo(points[s]);
                            if (slopeToQ == slopeToS) {
                                // Create the list of collinear points and sort them.
                                List<Point> collinearPoints = new ArrayList<Point>(4);
                                collinearPoints.add(points[p]);
                                collinearPoints.add(points[q]);
                                collinearPoints.add(points[r]);
                                collinearPoints.add(points[s]);
                                Collections.sort(collinearPoints);
                                segments(Collections.min(collinearPoints), Collections.max(collinearPoints)); //this is where the error is

                            }
                        }
                    }
                }
            }
       }
   }
   //public           int numberOfSegments();        // the number of line segments
   public LineSegment[] segments()                // the line segments
   {  
        return segments();
   }
}
4

2 に答える 2

0

メソッドは次のsegmentsように定義されます。

public LineSegment[] segments()                // the line segments
{  
    return segments();
}

これは、メソッドが引数を取らないにもかかわらず、2 つの引数で呼び出していることを意味します。

segments(Collections.min(collinearPoints), Collections.max(collinearPoints));

これを修正するには、次segmentsのように引数なしでメソッドを呼び出す必要があります。

segments();

または、メソッドを再定義して、引数を処理します。例えば:

public LineSegment[] segments(Point a, Point b)
{  
    // calculate line segments from a to b and return them
}

余談ですが、現在定義されているsegmentsメソッドは、終了条件なしで再帰的に呼び出しているため、確実に機能しません。

于 2016-04-02T15:41:34.660 に答える