使用可比 class 保留 2D Arraylist 的原始索引
Keep original index of 2D Arraylist using comparable class
我有一个数组列表,它存储点之间的距离。在对 arraylist 进行排序后,我需要保留距离的原始索引,因为我需要在代码的后期找到 K 最近邻。我实现的 class 没有输出正确的距离索引,而只是 arraylists 的索引是输出
我已经尝试实现一个 2D 可比较的 class 但我遇到了一些错误。
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 *****
}
}
旁注:将来,创建一个最小的 运行nable 程序来演示问题,我们可以轻松地编译和 运行。对于这个问题,我必须自己创建一个:
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 + " ");
}
}
}
但在未来,您会希望这样做以帮助您的问题更容易得到解答。
我有一个数组列表,它存储点之间的距离。在对 arraylist 进行排序后,我需要保留距离的原始索引,因为我需要在代码的后期找到 K 最近邻。我实现的 class 没有输出正确的距离索引,而只是 arraylists 的索引是输出
我已经尝试实现一个 2D 可比较的 class 但我遇到了一些错误。
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 *****
}
}
旁注:将来,创建一个最小的 运行nable 程序来演示问题,我们可以轻松地编译和 运行。对于这个问题,我必须自己创建一个:
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 + " ");
}
}
}
但在未来,您会希望这样做以帮助您的问题更容易得到解答。