Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中数组列表的排序_Java - Fatal编程技术网

Java中数组列表的排序

Java中数组列表的排序,java,Java,我有以下课程。在这里,Iris是另一个具有某些属性的类 public class Helper { Iris iris; double distance; public Helper(Iris iris, double distance) { this.iris = iris; this.distance = distance; } } 我想对这个数组列表进行排序(即listhelperList),根据距离参数降序。我已经编写

我有以下课程。在这里,Iris是另一个具有某些属性的类

public class Helper {

    Iris iris;
    double distance;

    public Helper(Iris iris, double distance) {
        this.iris = iris;
        this.distance = distance;
    }
}
我想对这个数组列表进行排序(即listhelperList),根据距离参数降序。我已经编写了以下方法,但它不起作用

public void sort(){
for(int k=0; k < helperList.size(); k++)
        {
            double distance = helperList.get(k).distance; 

             for(int l=0; l < helperList.size(); l++)
             {
                 Helper temp = helperList.get(l);
                 if( distance < temp.distance )
                 {
                     helperList.set(l, helperList.get(k));
                     helperList.set(k, temp);
                 }
            }
        }
}
public void sort(){
for(int k=0;k

有人能提出一个解决方案吗?

为什么不让您的
助手
类实现
Compariable
接口,然后使用Collections类提供的内置排序方法呢

Collections.sort(helperList) 
我认为这会解决问题。另外,这种排序方法是稳定的

实现可比接口:

public class Helper implements Comparable{

    Iris iris;
    double distance;

    public Helper(Iris iris, double distance) {
        this.iris = iris;
        this.distance = distance;
    }

    public int compareTo(Helper other) {
        return new Double(this.distance).compareTo(new Double(other.distance));

    }
}
维基百科上的文章包含伪代码和一些优化版本。和那个比较一下,看看你错在哪里


冒泡排序是最明显的排序算法之一,但并不是最有效的。你为什么不让平台来做呢
java.util.Collections
包含一个方法,允许您提供自己的方法。比较器所要做的就是决定两个
Helper
实例中的哪一个应该首先出现。

问题在于,在交换距离索引后,循环会失去距离索引所在位置的跟踪。这个算法应该运行良好

 for(int k = 1; k < helperList.size(); k++) {
    double distance = helperList.get(k).distance;
    int j = k - 1;
    boolean done = false;
    while(!done) {
       Helper temp = helperList.get(j);
       if(temp.distance < distance) {
           helperList.set(j+1, temp);
           j = j - 1;
           if(j < 0) {
              done = true;
           }
       } else {
          done = true;
       }
       helperList.set(j+1, value);
   }
}
for(int k=1;k
很好,但如果您不想实现类似的接口,以下内容可能会有所帮助:

Collections.sort(helperList, new Comparator<Helper>() {
    public int compare(Helper helper1, Helper helper2) {
        return Double.compare(helper1.distance, helper2.distance);
    }
})
Collections.sort(helperList,newcomparator(){
public int compare(助手helper1、助手helper2){
返回Double.compare(helper1.distance,helper2.distance);
}
})

< /代码>它不工作的方式是什么?它没有正确地对列表进行排序。考虑两个元素会发生什么情况,如<代码> [1,2] < /代码>。对于
k=0,l=1
,因为
list.get(1)>1
,您交换,给出
[2,1]
。然后,对于
k=1,l=0
再次交换。您应该只将每个元素与它一侧的元素进行比较。@Daniel Fischer!在我的例子中,列表由120个元素组成。一半以上的起始元素排序正确,但一些结尾元素排序不正确。我无法理解这种行为。因为你在来回交换。我还没有分析过,也许如果你从
k+1而不是0开始内部循环,它就会工作。但是,正确的方法是创建一个满足您需要的
比较器
,并使用库排序。或者实现一个经典的排序算法。如果两个距离的间隔小于1,则此compareTo实现将返回0(如果您正确地进行强制转换)。返回this.distance其他距离?1 : 0;它起作用了。感谢所有人,特别是@Divya的建议。为什么每次进行比较时都要创建两个双对象?即使您愿意,也可以使用Double.valueOf()。当您添加到javadoc的链接时,请尝试包含最新的链接:)
helper1.distance-helper2.distance
给出一个Double而不是int。我犯了同样的错误。Thanx Shawn。我使用了Collections.sort(object),正如Divya所建议的那样,效果很好。