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

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

级别: 新人
发帖
7
云币
14
=8BMCedH|  
ava如何对ArrayList中对象按照该对象某属性排序 c zTr_>  
(从小到大) p&'oJy.P  
两种方法: $|~YXH~O  
$*EK v'g[n  
7!.#:+rg5#  
[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;            }        }     }; o'nju.'  
M(>"e*Pi  
调用方法: ~RdJP'YF-  
w6ZyMR,T  
$RA8U:Q!1e  
[tr=none]
1
2
3
List<KNNNode> nodelist = new ArrayList<KNNNode>();//此处需要填充nodelist.add();Collections.sort(nodelist, comparator);// 调用 HPM ggRs  
E#'JYz@  
Th&-n%r9K  
x;d*?69f]  
uA]Z"  
OhUEp g[  
[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;            }        }    } 9I3vW]0x[  
xb`,9.a7  
调用方法: $Pw@EC]  
?"oW1a\  
R%7* )3$&r  
[tr=none]
1
Collections.sort(nodelist, new SortBy()); Y`$dtg {  
KNNNode结点内的属性: alu3CE  
?[~"$  
Rw8l"`  
[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;    }     } 3Yu1ZuIR  
我想要按照KNNNode的distance属性排序。 JIeKp7;^  
)a:j_jy  
u~kfz*hz  
若要按照从大到小的顺序排序,如果只是将 V5 $J  
>=返回值改为-1 6 <&jY  
<返回值改为1 会出现异常。 (N6 3k1M  
Comparison method violates its general contract! $nX4!X  
原因是是实现接口Comparator 的类有问题在compare中没有处理好两个比较的对象相等的情况 <zmtVE*>g  
public int compare(KNNNode o1, KNNNode o2);即 o1.getDistance() == o2.getDistance() 时,返回值不是0 ldEZ_g^  
我最终的实现代码是:(从大到小排序) P3[!-sv  
 {@XzY>  
'3B\I#  
[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 字节
如果您在写长篇帖子又不马上发表,建议存为草稿
 
验证问题: 37 - 24 = ?
上一个 下一个