java—如何比较列表和方法中的值?

y4ekin9u  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(343)

我有一个linkedlist,其中包含不同类型的数据,我需要处理这些数据,以便进行比较并添加符合范围的值。下面将给出更多解释, LinkedList 填充了记录类的数据:

class Record {
    public int id;
    public Point location; 
    public double score; 
    (...)
}

点类:

class Point {
    public double x, y;
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    public double dist(Point p) {
        return Math.sqrt((this.x-p.x)*(this.x-p.x)+(this.y-p.y)*(this.y-p.y));
    }

等级:

class RankList {

    private Node first;
    private int nodeCount;
    private Record record;

    public static void main(String[] args) {
        RankList list = new RankList();
        Point point = new Point(5.4, 3.2);
        Record record = new Record(1, point, 8.2);
        System.out.println(list.insert(record));
        double maxDist=point.dist(point);

        Point point1 = new Point(1.4, 9.2);
        Record record1 = new Record(2, point1, 7.5);
        if((point1.dist(point)>maxDist)) maxDist=point1.dist(point);
        System.out.println(list.insert(record1));

        Point point2 = new Point(2.2, 1.2);
        Record record2 = new Record(3, point2, 6.0);
        if((point2.dist(point1)>maxDist)) maxDist=point2.dist(point1);
        System.out.println(list.insert(record2));
        list.nearest(point1,maxDist);

我在列表中插入了一些值,假设给定点之间有一些距离值,比如:

Distance between two points:
A->B = 3.2455
B->C = 7.345 
C->D = 2.111 
D->E = 8.056

从那以后 maxDist 值为8.059
现在我要写方法了 public RankList nearest (Point p,double maxDist) 它查找作用域(<=maxdist)之间的所有距离值,并将它们与其余节点值一起返回到一个列表中。所以我需要用linkedlist的指针和给定的 Point p 参数,并将它们添加到新列表中。
我的问题是我是否可以访问 LinkedList 它已经用值实现了,并将我需要的复制到新的列表结构中。
最近的方法:

public RankList nearest (Point p,double maxDist){
            RankList nearList = new RankList();
            Node current = first;
            System.out.print("HEAD -> ");
            while (current != null) {
                System.out.print(current);
                System.out.print(" -> ");
                current = current.getNext();
            }
            System.out.println("null");
            return null;
       }

我试着把整个 LinkedList 用传统的方法,但我会把它们放在如何进行比较并添加到新的列表中。
有什么建议吗?

zbq4xfa0

zbq4xfa01#

我对你的代码有点困惑。我找不到 LinkedList 在里面。你的班级 RankList 似乎只有三个字段,而且都不是列表类型。我不知道这是怎么回事 insert 方法确实如此。
在类中使用 main 方法。如果你准备另一个运行这个程序的类会更好。
我想展示一下如何编写代码:

import java.util.Comparator;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

class Main {

    public static void main(String[] args) {
        // I don't want the list to be a field of the class.
        RecordsList list = new RecordsList();

        // Following code blocks could be extracted as separate methods
        Point point = new Point(5.4, 3.2);
        Record record = new Record(1, point, 8.2);
        list.add(record);
        double maxDist = point.dist(point);

        Point point1 = new Point(1.4, 9.2);
        Record record1 = new Record(2, point1, 7.5);
        if((point1.dist(point)>maxDist)) maxDist=point1.dist(point);
        list.add(record1);

        Point point2 = new Point(2.2, 1.2);
        Record record2 = new Record(3, point2, 6.0);
        if((point2.dist(point1)>maxDist)) maxDist=point2.dist(point1);
        list.add(record2);

        Point farPoint = new Point(50, 50);
        Record recordWithFarPoint = new Record(4, farPoint, 5);
        list.add(recordWithFarPoint);

        RecordsList nearestList = list.nearest(point1, 20);
        for (Record rec : nearestList.getList()) {
            System.out.println(rec.id + " " + rec.location.x + " " + rec.location.y + " " + rec.score);
        }
        /*
         * On console:
         * 2 1.4 9.2 7.5
         * 1 5.4 3.2 8.2
         * 3 2.2 1.2 6.0
         */
    }

}

/**
 * This class is so-called wrapper on ArrayList.
 */
class RecordsList {
    // This may be called delegate.
    private ArrayList<Record> list = new ArrayList<>();

    public void add(Record record) {
        this.list.add(record);
    }

    // This creates shallow copy.
    public ArrayList<Record> getList() {
        return new ArrayList<>(list);
    }

    public RecordsList nearest(Point p, double maxDistance) {
        RecordsList list = new RecordsList();

        List<Record> records = this.getList().stream()
                .sorted(Comparator.comparingDouble(oldListElement -> oldListElement.location.dist(p)))
                .filter(element -> element.location.dist(p) <= maxDistance)
                .collect(Collectors.toList());

        for (Record record : records) {
            list.add(record);
        }

        return list;
    }

}

class Record {
    public int id;
    public Point location;
    public double score;

    public Record(int id, Point location, double score) {
        this.id = id;
        this.location = location;
        this.score = score;
    }

}

class Point {

    public double x, y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double dist(Point p) {
        return Math.sqrt((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y));
    }

}

有些概念对你来说可能是新的。最复杂的一个可能是streamapi。您可以阅读更多信息,即:https://www.baeldung.com/java-8-streams
如果你有任何问题,或者我误解了你的问题,请尽管问。

相关问题