你是否正在寻找关于arraylist排序的内容?让我把最实时的东西奉献给你:
常常遇到数组排序的问题.比如我有一个Person类,它的实例对象存储在ArrayList数组中,现在要把ArrayList数组中的Person对象按照年龄排序.
其实这种情况经常遇到.
下面给出源代码:
1:Person.java文件:-------------------------------
public class Person{
String name;
int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2:Mycomparator.java-------------------------------
//实现Comparator接口,也就是定义排序规则,你几乎可以定义任何规则
package com.infoearth;
import java.util.*;
public class Mycomparator implements Comparator{
public int compare(Object o1,Object o2) {
Person p1=(Person)o1;
Person p2=(Person)o2;
if(p1.age
以上就是关于arraylist排序的全部内容,相信你一定会非常满意,。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-18185-1.html
允许一妻多夫
那日本完全不能抵挡了