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

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

级别: 新人
发帖
7
云币
14
nwp(% fBo  
ava如何对ArrayList中对象按照该对象某属性排序 }v's>Ae~p  
(从小到大) #R>x]Nt}  
两种方法: hrm<!uKn  
43pe6 ^.  
NJ)Dw`|%|)  
[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;            }        }     }; Kp6 @?  
59O-"Sc[  
调用方法: +i!5<nn  
00.x*v  
{G Jl<G1  
[tr=none]
1
2
3
List<KNNNode> nodelist = new ArrayList<KNNNode>();//此处需要填充nodelist.add();Collections.sort(nodelist, comparator);// 调用 {g! 7K  
{Q3OT  
1!KROes4  
f4h~c  
R>* z8n  
~<?Zj  
[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;            }        }    } uYMn VE"  
b2OwLt9  
调用方法: (T_-`N|  
@"@|O>KJ  
B;r_[^  
[tr=none]
1
Collections.sort(nodelist, new SortBy()); n@8Y6+7i  
KNNNode结点内的属性: V 9Qt;]mQ  
]~aj  
nzC *mPX8  
[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;    }     } YGJ!!(~r  
我想要按照KNNNode的distance属性排序。 gx.\H3y  
1'qXT{f/~  
]E-/}Ysz  
若要按照从大到小的顺序排序,如果只是将 w7Yu} JY^  
>=返回值改为-1 N4vcd=uG#  
<返回值改为1 会出现异常。 SZF 8InyF  
Comparison method violates its general contract! k24I1DlR8  
原因是是实现接口Comparator 的类有问题在compare中没有处理好两个比较的对象相等的情况 X5iD <Lh  
public int compare(KNNNode o1, KNNNode o2);即 o1.getDistance() == o2.getDistance() 时,返回值不是0 .o/|]d`%  
我最终的实现代码是:(从大到小排序) <  o?ua}  
:qdyC sn2  
$`|h F[tv  
[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是阿里云提供的什么服务? 正确答案:云服务器
上一个 下一个