使用可比类保留 2D Arraylist 的原始索引

我有一个数组列表的数组列表,它存储点之间的距离。在对 arraylist 排序后,我需要保留距离的原始索引,因为我需要在代码的后期找到 K 最近邻。我实现的类没有输出正确的距离索引,而只是输出了数组列表的索引


我尝试实现一个 2D 可比类,但出现了一些错误。


public staticArrayList<ArrayList<Double>>distance(ArrayList<sample_points> points) {


    ArrayList<ArrayList<Double>> distArray = new ArrayList<ArrayList<Double>>(points.size());

    double dist = 0;

    List<Element> elements = new ArrayList<Element>();

    for(int i = 0; i<points.size()-1; i++) {

        ArrayList<Double> distances = new ArrayList<Double>();

        for(int j=i+1; j<points.size(); j++){

            // do your calculations here

            dist = Math.sqrt(Math.pow(points.get(i).getX() - points.get(j).getX(), 2) 

                    + Math.pow(points.get(i).getY() - points.get(j).getY(), 2));   

            distances.add(dist);// add the distance to the current distances list

        }

        distArray.add(distances);//

    }

    System.out.print("Distances: "distArray);

    System.out.println();

    for(int i = 0; i < distArray.size(); i++) {

        for (int j = 0; j < distArray.get(i).size(); j++) {

            elements.add(new Element(i, distArray.get(i).get(j)));

        }

    }

    Collections.sort(elements);

    for(int i = 0; i < elements.size(); i++) {

        System.out.println("Dist "+ elements.get(i).distance + " " 

                + "Index " + elements.get(i).index+" ");

    }

}

这是我的可比类:


class Element implements  Comparable<Element>{

    public final int index;

    public final double distance;


    public Element(int index, double distance){

        this.index = index;

        this.distance = distance;

    }


    @Override

    public int compareTo(Element e){

        return   Double.valueOf(this.distance).compareTo(Double.valueOf(e.distance));

    }

}

预期成绩:


Distances: [[2.8284271247461903, 5.830951894845301, 7.280109889280518], 

[3.1622776601683795, 5.0], [2.23606797749979]]


Dist 2.23606797749979 Index 0 

Dist 2.8284271247461903 Index 0 

Dist 3.1622776601683795 Index 0 

Dist 5.0 Index 1 

Dist 5.830951894845301 Index 1 

Dist 7.280109889280518 Index 2




Helenr
浏览 124回答 1
1回答

慕桂英546537

在审查您的代码时,我认为错误出在您的假设中,并且输出本身实际上是正确的。您看到的索引实际上是您在此处创建的列表嵌套数组列表列表中外部列表的索引:for (int i=0; i<points.size()-1; i++) {&nbsp; &nbsp; ArrayList<Double> distances = new ArrayList<Double>();&nbsp; &nbsp; for (int j=i+1; j < points.size(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; // do your calculations here&nbsp; &nbsp; &nbsp; &nbsp; dist = Math.sqrt(Math.pow(points.get(i).getX() - points.get(j).getX(), 2)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + Math.pow(points.get(i).getY() - points.get(j).getY(), 2));&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; distances.add(dist); // add the distance to the current distances list&nbsp; &nbsp; }&nbsp; &nbsp; distArray.add(distances);&nbsp; //&nbsp; ***** the index of items added here *****}如果添加这些调试行:for (int i=0; i < points.size()-1; i++) {&nbsp; &nbsp; ArrayList<Double> distances = new ArrayList<Double>();&nbsp; &nbsp; for (int j=i+1; j<points.size(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; // do your calculations here&nbsp; &nbsp; &nbsp; &nbsp; dist = Math.sqrt(Math.pow(points.get(i).getX() - points.get(j).getX(), 2)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + Math.pow(points.get(i).getY() - points.get(j).getY(), 2));&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; distances.add(dist);// add the distance to the current distances list&nbsp; &nbsp; }&nbsp; &nbsp; distArray.add(distances);&nbsp; //&nbsp; ***** the index of items added here *****&nbsp; &nbsp; // ******* add this&nbsp; ********&nbsp; &nbsp; System.out.println(distArray.indexOf(distances));&nbsp; &nbsp; System.out.println(distances);&nbsp; &nbsp; System.out.println();}你会发现这是正确的。您需要改变创建指数值的方式或改变您的假设。此外,您的元素索引字段是不可变的,并且是在对距离 ArrayList 中的任何项目进行排序之前创建的,因此该字段不能表示距离的排序顺序。您在评论中声明:如果我有一个距离的二维数组列表,即 [ [d1,d2,d3], [d4,d5], [d6] ]。指数将如下d1=0、d2=1、d3=2、d4=0、d5=1和d6=0。所以我需要在对每个 arraylist 进行排序后获取原始索引。然后用来j创建索引,不ifor(int i = 0; i < distArray.size(); i++) {&nbsp; &nbsp; for (int j = 0; j < distArray.get(i).size(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; // elements.add(new Element(i, distArray.get(i).get(j)));&nbsp; &nbsp; &nbsp; &nbsp; elements.add(new Element(j, distArray.get(i).get(j))); // ***** note change *****&nbsp; &nbsp; }}旁注:将来,创建一个最小的可运行程序来演示问题,我们可以轻松编译和运行。对于这个问题,我必须自己创建一个:import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Foo01 {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; List<SamplePoints> points = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; int maxXY = 100;&nbsp; &nbsp; &nbsp; &nbsp; int max = 4;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < max; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int x = (int) (maxXY * Math.random());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int y = (int) (maxXY * Math.random());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; points.add(new SamplePoints(x, y));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; distance(points);&nbsp; &nbsp; }&nbsp; &nbsp; private static class SamplePoints {&nbsp; &nbsp; &nbsp; &nbsp; private int x;&nbsp; &nbsp; &nbsp; &nbsp; private int y;&nbsp; &nbsp; &nbsp; &nbsp; public SamplePoints(int x, int y) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.x = x;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.y = y;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public int getX() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return x;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public int getY() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return y;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static class Element implements Comparable<Element> {&nbsp; &nbsp; &nbsp; &nbsp; public final int index;&nbsp; &nbsp; &nbsp; &nbsp; public final double distance;&nbsp; &nbsp; &nbsp; &nbsp; public Element(int index, double distance) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.index = index;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.distance = distance;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public int compareTo(Element e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Double.valueOf(this.distance).compareTo(Double.valueOf(e.distance));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static void distance(List<SamplePoints> points) {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<ArrayList<Double>> distArray = new ArrayList<ArrayList<Double>>(points.size());&nbsp; &nbsp; &nbsp; &nbsp; double dist = 0;&nbsp; &nbsp; &nbsp; &nbsp; List<Element> elements = new ArrayList<Element>();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < points.size() - 1; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Double> distances = new ArrayList<Double>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = i + 1; j < points.size(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do your calculations here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dist = Math.sqrt(Math.pow(points.get(i).getX() - points.get(j).getX(), 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + Math.pow(points.get(i).getY() - points.get(j).getY(), 2));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; distances.add(dist);// add the distance to the current distances&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; distArray.add(distances);//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(distArray.indexOf(distances));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(distances);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Distances: " + distArray);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < distArray.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < distArray.get(i).size(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elements.add(new Element(i, distArray.get(i).get(j)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(elements);&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < elements.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Dist " + elements.get(i).distance + " " + "Index "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + elements.get(i).index + " ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}但在未来,您将希望这样做以帮助您的问题更容易回答。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java