1 回答
TA贡献1848条经验 获得超10个赞
在审查您的代码时,我认为错误出在您的假设中,并且输出本身实际上是正确的。您看到的索引实际上是您在此处创建的列表嵌套数组列表列表中外部列表的索引:
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 中的任何项目进行排序之前创建的,因此该字段不能表示距离的排序顺序。
您在评论中声明:
如果我有一个距离的二维数组列表,即 [ [d1,d2,d3], [d4,d5], [d6] ]。指数将如下d1=0、d2=1、d3=2、d4=0、d5=1和d6=0。所以我需要在对每个 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 + " ");
}
}
}
但在未来,您将希望这样做以帮助您的问题更容易回答。
添加回答
举报