首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型BruteCollinearPoints中的方法segments()不适用于参数(Point,Point)

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

Stack Overflow用户
提问于 2016-04-02 23:27:47
回答 2查看 713关注 0票数 0

我正在尝试修改代码,但得到了title.Anyone中列出的错误,请帮助。

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

代码语言:javascript
复制
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();
   }
}
EN

回答 2

Stack Overflow用户

发布于 2016-04-02 23:41:35

segments方法的定义如下:

代码语言:javascript
复制
public LineSegment[] segments()                // the line segments
{  
    return segments();
}

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

代码语言:javascript
复制
segments(Collections.min(collinearPoints), Collections.max(collinearPoints));

要解决此问题,您需要调用不带参数的segments方法,如下所示:

代码语言:javascript
复制
segments();

或者重新定义该方法以使用参数。例如:

代码语言:javascript
复制
public LineSegment[] segments(Point a, Point b)
{  
    // calculate line segments from a to b and return them
}

方面,,正如现在定义的那样,segments方法肯定不会工作,因为它在没有终止条件的情况下递归地调用自己。

票数 0
EN

Stack Overflow用户

发布于 2017-02-25 06:20:00

从您的导入语句可以看出,您正在处理来自Algorithms, 4th Edition course的一项编程任务,称为模式识别。在这个过程中,segments()方法从另一个类返回一个LineSegment[]类型的数组。您可以在提供的“示例客户端程序”中看到它的用法。以下是其中的一部分:

代码语言:javascript
复制
for (LineSegment segment : collinear.segments()) {
    StdOut.println(segment);
    segment.draw();
}
StdDraw.show();

为了实际创建一个新的LineSegment,使用如下代码:

代码语言:javascript
复制
LineSegment seg = new LineSegment(point1, point2)

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

代码语言:javascript
复制
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;
    }
.
.
.
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36375137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档