在Java中,如何使方法具有Iterable返回类型? [关闭]

问题描述 投票:0回答:1

所以我是编码新手。我已经完成了网页设计和较小的项目,但它们大多都很简单。我的朋友推荐我在此处尝试在Stack Overflow上提问。

I recently came across the following

所以直到几个月前,我还没有在Java中研究过这种程度的算法。我正在尝试自学Java和计算机科学的更多理论方面。我想证明自己(和我的某个熟识的人)能够做到。但是,我已经感到沮丧,无法继续尝试。

这是我到目前为止所拥有的:

package ColinearPt;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class BruteCollinearPoints {
    Point[] pointArray;
    ArrayList<PointSequence> pSeqArr = new ArrayList<>();

    public BruteCollinearPoints(Point[] points) {
        if (points == null)
            throw new NullPointerException();

        pointArray = points.clone();
        Arrays.sort(pointArray);

        for (int p = 0; p < pointArray.length - 3; p++) {
            for (int q = p + 1; q < pointArray.length - 2; q++) {
                for (int r = q + 1; r < pointArray.length - 1; r++) {
                    for (int s = r + 1; s < pointArray.length; s++) {
                        Point[] four = { pointArray[p], pointArray[q], pointArray[r], pointArray[s] };
                        PointSequence pseq = new PointSequence(four);
                        if (pseq.isCollinear()) {
                            pSeqArr.add(pseq);
                        }
                    }
                }
            }
        }
    }
    //makes a defensive copy of the array of points

    public int numberOfPoints() {
        //returns the number of total points in the array

        return pointArray.length;
        //numberOfPoints();
    }

    public int numberOfSegments() {
        return pSeqArr.size();
        //returns the number of segments of length 4
    }

    public Iterable<PointSequence> segments() {
        //returns an iterable of segments of length 4
    }

    public static void main(String[] args) {
        //draws all 4 point segments in file
    }
}

我对segments()特别困惑,由于我不知道所有测试细节,所以我什至不确定我要寻找什么。我一直在阅读Iterables,但我仍然感到困惑。我真的很感激有关此特定项目的任何技巧,甚至只是如何自己学习计算机科学的技巧。

java algorithm iterable
1个回答
1
投票

因为在内部是Collection<E> extends Iterable<E>,所以您可以简单地将罐子收藏转换为Iterable

如下所示:

public Iterable<PointSequence> segments() {
    //returns an iterable of segments of length 4
    List<PointSequence> pSeqArr = new ArrayList<>();
    //add your logic
    return pSeqArr;
}
© www.soinside.com 2019 - 2024. All rights reserved.