0

我正在尝试修改代码,但得到标题中列出的错误。请帮助。

错误:BruteCollinearPoints 类型中的方法 segments() 不适用于参数 (Point, Point)

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();
}

这意味着该方法不接受任何参数,但您使用两个参数调用它:

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 回答
0

从您的导入语句来看,您似乎正在处理算法第四版课程中称为模式识别的编程作业。在那个过程中,该方法从另一个类segments()返回一个类型的数组。LineSegment[]您可以看到它在提供的“示例客户端程序”中使用。这是其中的一部分:

for (LineSegment segment : collinear.segments()) {
    StdOut.println(segment);
    segment.draw();
}
StdDraw.show();

为了真正创建一个新的LineSegment,使用这样的东西:

LineSegment seg = new LineSegment(point1, point2)

作为参考,这里是调用的创建者LineSegment.java

public class LineSegment {
private final Point p;   // one endpoint of this line segment
private final Point q;   // the other endpoint of this line segment

/**
 * Initializes a new line segment.
 *
 * @param  p one endpoint
 * @param  q the other endpoint
 * @throws NullPointerException if either p or q is null
 */
    public LineSegment(Point p, Point q) {
        if (p == null || q == null) {
            throw new NullPointerException("argument is null");
        }
        this.p = p;
        this.q = q;
    }
.
.
.
}
于 2017-02-24T22:20:00.257 回答