阿里云
发表主题 回复主题
  • 129阅读
  • 0回复

[交流乐园]java中对ArrayList进行排序

级别: 新人
发帖
7
云币
14
efZdtrKgy  
ava如何对ArrayList中对象按照该对象某属性排序 w[:5uo(  
(从小到大) tJ2l_M^  
两种方法: rQN+x|dKMb  
NM[w=  
klv^310  
[tr=none]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
方法一:<br data-filtered="filtered">Comparator<KNNNode> comparator = new Comparator<KNNNode>() {         @Override        public int compare(KNNNode o1, KNNNode o2) {            System.out.println("进入 compare function");            // TODO Auto-generated method stub            if (o1.getDistance() <= o2.getDistance()) {//o1属性小于等于o2属性时,返回1                return 1;            } else {                return -1;            }        }     }; 0z.`  
tH17Z  
调用方法: aH,0+|  
ag[yM  
c\]h YKA  
[tr=none]
1
2
3
List<KNNNode> nodelist = new ArrayList<KNNNode>();//此处需要填充nodelist.add();Collections.sort(nodelist, comparator);// 调用 4>$>XL1  
*N< 22w  
78y4nRQ*  
/rKrnxw  
>:KPvq!0  
$].< /  
[tr=none]
1
2
3
4
5
6
7
8
9
10
11
方法二:<br>class SortBy implements Comparator {        public int compare(Object obj1, Object obj2) {            KNNNode point1 = (KNNNode) obj1;            KNNNode point2 = (KNNNode) obj2;            if (point1.getDistance() <= point2.getDistance()) {                return 1;            } else {                return 0;            }        }    } ;y4 "wBX  
VUagZ 7p  
调用方法: vA(3H/)-  
g&riio7lx  
B7N?"'$i  
[tr=none]
1
Collections.sort(nodelist, new SortBy()); %w' @:~0  
KNNNode结点内的属性: yd`.Rb&V  
jO!!. w  
YNk?1#k?i  
[tr=none]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package newKNN; /** * KNN结点类,用来存储最近邻的k个元组相关的信息 */public class KNNNode {    private int index; // 元组标号    private double distance; // 与测试元组的距离    private String seq;//滑动窗口motif序列片段         public KNNNode(int index, double distance, String seq) {        super();        this.index = index;        this.distance = distance;        this.seq = seq;             }    public int getIndex() {        return index;    }    public void setIndex(int index) {        this.index = index;    }    public double getDistance() {        return distance;    }    public void setDistance(double distance) {        this.distance = distance;    }    public String getSeq() {        return seq;    }    public void setSeq(String seq) {        this.seq = seq;    }     } .CwMxuW  
我想要按照KNNNode的distance属性排序。 G)'cd D1  
n8R{LjJ2@  
B5J=q("P  
若要按照从大到小的顺序排序,如果只是将 [$-y8`~(  
>=返回值改为-1 .VTHZvyn  
<返回值改为1 会出现异常。 mw?,oiT,)  
Comparison method violates its general contract! ]#+fQR$!  
原因是是实现接口Comparator 的类有问题在compare中没有处理好两个比较的对象相等的情况 6dq5f?w]  
public int compare(KNNNode o1, KNNNode o2);即 o1.getDistance() == o2.getDistance() 时,返回值不是0 khW3z*e#  
我最终的实现代码是:(从大到小排序) HQv#\Xi1  
olQ;XTa01F  
m:X;dcq'3  
[tr=none]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Comparator<KNNNode> comparator = new Comparator<KNNNode>() {         @Override        public int compare(KNNNode o1, KNNNode o2) {            // TODO Auto-generated method stub            if (o1.getDistance() < o2.getDistance()) {                return 1;            } else if(o1.getDistance() > o2.getDistance()){                return -1;            }else{                return 0;            }        }     };
关键词: java
发表主题 回复主题
« 返回列表上一主题下一主题

限100 字节
如果您在写长篇帖子又不马上发表,建议存为草稿
 
验证问题: 4 - 4 = ?
上一个 下一个