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

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

级别: 新人
发帖
7
云币
14
{:6VJ0s\  
ava如何对ArrayList中对象按照该对象某属性排序 S_?}H  
(从小到大) qf)]!w U9  
两种方法: eDaVoc3  
]PUyX8'~  
ucoBeNsHx  
[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;            }        }     }; Oo7n_h1  
%NeKDE  
调用方法: m?[F)<~a  
i.F[.-.  
Dm 'Q&  
[tr=none]
1
2
3
List<KNNNode> nodelist = new ArrayList<KNNNode>();//此处需要填充nodelist.add();Collections.sort(nodelist, comparator);// 调用 4RTEXoXs  
/^4)V8D_S  
W!IK>IW"  
Bct>EWQ  
-(:BkA  
/x$jd )C  
[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;            }        }    } OY[N%wr!  
+pf 7  
调用方法: `uzRHbJ`  
&X|z(vSJ$  
c:=Z<0S;  
[tr=none]
1
Collections.sort(nodelist, new SortBy()); E(u[?  
KNNNode结点内的属性: 6GJ?rE E/  
W9eR3q  
zlSwKd(  
[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;    }     } <Xl G:nmY  
我想要按照KNNNode的distance属性排序。 {`J7>K  
(q +Q.Q  
+ FLzK(  
若要按照从大到小的顺序排序,如果只是将 xhimRi  
>=返回值改为-1 OsC1('4@  
<返回值改为1 会出现异常。 #}k^g:l1  
Comparison method violates its general contract! ?dxhe7m  
原因是是实现接口Comparator 的类有问题在compare中没有处理好两个比较的对象相等的情况 r-'j#|^tz  
public int compare(KNNNode o1, KNNNode o2);即 o1.getDistance() == o2.getDistance() 时,返回值不是0 56TUh_  
我最终的实现代码是:(从大到小排序) 0 'L+9T5  
 \f  
OM, uR3,  
[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 字节
如果您在写长篇帖子又不马上发表,建议存为草稿
 
验证问题: ECS是阿里云提供的什么服务? 正确答案:云服务器
上一个 下一个