gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:31:07 25 4
gpt4 key购买 nike

我有一个数组列表的数组列表,它存储点之间的距离。对数组列表进行排序后,我需要保留距离的原始索引,因为我需要在代码的后期找到 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):

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

实际结果:

Distances: [[2.8284271247461903, 5.830951894845301, 7.280109889280518], 
[3.1622776601683795, 5.0], [2.23606797749979]]

Dist 2.23606797749979 Index 2
Dist 2.8284271247461903 Index 0
Dist 3.1622776601683795 Index 1
Dist 5.0 Index 1
Dist 5.830951894845301 Index 0
Dist 7.280109889280518 Index 0

最佳答案

在审查您的代码时,我认为错误在于您的假设,并且输出本身实际上是正确的。您看到的索引实际上是您在此处创建的列表嵌套数组列表列表中外部列表​​的索引:

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); // ***** the index of items added here *****
}

如果添加这些调试行:

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); // ***** the index of items added here *****

// ******* add this ********
System.out.println(distArray.indexOf(distances));
System.out.println(distances);
System.out.println();
}

您会发现这是正确的。您需要更改创建索引值的方式或更改您的假设。

<小时/>

此外,您的元素索引字段是不可变的,并且是在距离 ArrayList 中的任何项目排序之前创建的,因此该字段不能表示距离的排序顺序.

<小时/>

您在评论中指出:

If I have a 2D Arraylist of distances i.e [ [d1,d2,d3], [d4,d5], [d6] ]. The indices will be as follows d1=0, d2 =1, d3 = 2, d4 =0, d5 =1 and d6 =0. So I need to get the original indices as mentioned after I sort each arraylist.

然后使用j创建索引,而不是i

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)));
elements.add(new Element(j, distArray.get(i).get(j))); // ***** note change *****
}
}
<小时/>

旁注:将来,创建一个最小的可运行程序来演示问题,我们可以轻松编译和运行。对于这个问题,我必须自己创建一个:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Foo01 {

public static void main(String[] args) {
List<SamplePoints> points = new ArrayList<>();

int maxXY = 100;
int max = 4;
for (int i = 0; i < max; i++) {
int x = (int) (maxXY * Math.random());
int y = (int) (maxXY * Math.random());
points.add(new SamplePoints(x, y));
}

distance(points);
}

private static class SamplePoints {
private int x;
private int y;

public SamplePoints(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

}

private static 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));
}
}

public static void distance(List<SamplePoints> 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.println(distArray.indexOf(distances));
System.out.println(distances);
System.out.println();

}
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 + " ");
}
}
}

但将来,您会希望这样做以使您的问题更容易回答。

关于java - 使用可比较的类保留 2D Arraylist 的原始索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57538566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com