计算各个点之间的距离并存储这些距离

Calculate distance between respective points and store these distances

我有一个将坐标点存储为对象的数组列表,我还创建了一个公式来计算这些点之间的距离。我需要将每个点及其各自的距离存储在 2D arraylist 中。例如,如果我有点 (1,2)、(3,4)、(5,6)、(7,8),我的 2D arraylist 应该为点 (1,2) 存储 3 个距离,为点 (3) 存储两个距离,4) 和点 (7,8) 的一个距离,如果点 (1,2) 是我的起点。

我尝试创建一个嵌套的 for 循环,它跟踪我的点当前所在的位置,同时根据访问的样本点在每个索引中添加距离。但是我得到了一个越​​界异常。我已验证所有点都已正确存储。

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

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

    double dist = 0;

    for(int i =0;i<points.size();i++){
        distArray.add(new ArrayList());
    }
     for (int i = 0; i<points.size(); i++) {
        //ArrayList<Double> distances = new ArrayList<>();  // convenience, you don't need the first loop only to populate the distArray lists
        for(int j=i+1; j<points.size(); j++){
            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)
            );   // do your calculations here
            distances.add(dist);   // add the distance to the current distances list
        }
        distArray.add(distances); // add the current distances list to the result list
    }
    System.out.print("Your distances: "+ distarray)
    return distArray;
}

预期结果: 如果样本点数为 4,即 (p1,p2),(p3,p4),(p5,6),(p7,p8) 其中 p(i) 是任意点:

您的距离:[[d1,d2,d3],[d4,d5],[d6]] 其中 d(i) 是任意距离

实际结果: 您的距离:[[d1,d2,d3,d4],[d1,d2,d3,d4],[d1,d2,d3,d4][d1,d2,d3,d4]]

假设列表 pointsn 个元素。然后你的for循环在这里 for (int i = 0; i <= points.size(); i++)将在i=n时结束。但是你不能访问索引n处的points,因为它不存在。因此 points.get(i) 将抛出 IndexOutOfBoundsException 一次 i==n (或 i==points.size())。

一步一步地查看您的指数。假设您的列表 points 包含 3 个元素:

// first iteration
i=0  
  j=1, j=2
// second iteration
i=1
  j=2
// third iteration
i=2
  // no j-loop because j=3 and 3 == points.size()
// fourth iteration
i=3 // that should not be possible because points has only 3 elements

让我试着为你稍微修改一下循环(编辑:我忘了说方法的其余部分也不同。例如 distArray 的初始化)

public static void main(String[] args) {
    //this is just for testing. 
    ArrayList<sample_points> points = new ArrayList<>();
    points.add(new sample_points(1D, 1D));
    points.add(new sample_points(3D, 3D));
    points.add(new sample_points(5D, 5D));
    ArrayList<ArrayList<Double>> distance = distance(points);
}


public static ArrayList<ArrayList<Double>> distance(ArrayList<sample_points> points) {
    ArrayList<ArrayList<Double>> distArray = new ArrayList<>();  //here the result list

    for (int i = 0; i < points.size() - 1; i++) {   // you actually don't need the last iteration because the j-loop won't run anyway
        ArrayList<Double> distances = new ArrayList<>();  // convenience, you don't need the first loop only to populate the distArray lists
        for (int j = i + 1; j < points.size(); j++) {
            Double 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)
            );   // do your calculations here
            distances.add(dist);   // add the distance to the current distances list
        }
        distArray.add(distances); // add the current distances list to the result list
    }
    System.out.print("Your distances: " + distArray);
    return distArray;
}

当我 运行 这个时,我得到:

Your distances: [[2.8284271247461903, 5.656854249492381], [2.8284271247461903]]

我没有检查结果,但它似乎是正确的

距离 (1,1) 到 (3,3) 是 2.82....
dist (1,1) 到 (5,5) 是 5.65....

dist (3,3) to (5,5) 也是 2.82....