java如何对ArrayList中对象按照该对象某属性排序
publicstaticvoiddisplay(Collectionc){Iteratorit=c.iterator();while(it.hasNext()){Obje...
public static void display (Collection c){
Iterator it = c.iterator ();
while (it.hasNext()){
Object o = it.next();
System.out.println(o);
}
}
public static void main(String[] args) {
Student stu1 = new Student (1,"zhangsan","male",18,"cs");
Student stu2 = new Student (2,"lisi","female",19,"cs");
Student stu3 = new Student (3,"wangwu","male",20,"cs");
Student stu4 = new Student (4,"zhaoliu","female",21,"cs");
Student stu5 = new Student (5,"xiaoming","male",22,"cs");
ArrayList<Student> List = new ArrayList<Student>();
List.add(stu1);
List.add(stu2);
List.add(stu3);
List.add(stu4);
List.add(stu5);
display(List);
熬增加排序功能,打印时:输出学生对象的时候,需要先按照年龄排序,如果年龄相同,则按照姓名排序,如果姓名也相同,则按照学号排序。
这个怎么实现的,请大虾们帮下忙!谢谢! 展开
Iterator it = c.iterator ();
while (it.hasNext()){
Object o = it.next();
System.out.println(o);
}
}
public static void main(String[] args) {
Student stu1 = new Student (1,"zhangsan","male",18,"cs");
Student stu2 = new Student (2,"lisi","female",19,"cs");
Student stu3 = new Student (3,"wangwu","male",20,"cs");
Student stu4 = new Student (4,"zhaoliu","female",21,"cs");
Student stu5 = new Student (5,"xiaoming","male",22,"cs");
ArrayList<Student> List = new ArrayList<Student>();
List.add(stu1);
List.add(stu2);
List.add(stu3);
List.add(stu4);
List.add(stu5);
display(List);
熬增加排序功能,打印时:输出学生对象的时候,需要先按照年龄排序,如果年龄相同,则按照姓名排序,如果姓名也相同,则按照学号排序。
这个怎么实现的,请大虾们帮下忙!谢谢! 展开
3个回答
展开全部
有几个方法可以实现:让 Student 实现Comparable接口,或是实例化一个比较器,
现在用 Comparator 比较器实例来做一个:ComparableTest.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class ComparableTest {
public static void main(String[] args) {
Comparator<Student> comparator = new Comparator<Student>(){
public int compare(Student s1, Student s2) {
//先排年龄
if(s1.age!=s2.age){
return s1.age-s2.age;
}
else{
//年龄相同则按姓名排序
if(!s1.name.equals(s2.name)){
return s1.name.compareTo(s2.name);
}
else{
//姓名也相同则按学号排序
return s1.id-s2.id;
}
}
}
};
Student stu1 = new Student (1,"zhangsan","male",28,"cs");
Student stu2 = new Student (2,"lisi","female",19,"cs");
Student stu3 = new Student (3,"wangwu","male",22,"cs");
Student stu4 = new Student (4,"zhaoliu","female",17,"cs");
Student stu5 = new Student (5,"jiaoming","male",22,"cs");
ArrayList<Student> List = new ArrayList<Student>();
List.add(stu1);
List.add(stu2);
List.add(stu3);
List.add(stu4);
List.add(stu5);
//这里就会自动根据规则进行排序
Collections.sort(List,comparator);
display(List);
}
static void display(ArrayList<Student> lst){
for(Student s:lst)
System.out.println(s);
}
}
class Student{
int age;
int id;
String gender;
String name;
String cs;
Student(int id,String name,String gender,int age,String cs){
this.age=age;
this.name=name;
this.gender=gender;
this.id=id;
this.cs=cs;
}
public String toString(){
return id+" "+name+" "+gender+" "+age+" "+cs;
}
}
现在用 Comparator 比较器实例来做一个:ComparableTest.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class ComparableTest {
public static void main(String[] args) {
Comparator<Student> comparator = new Comparator<Student>(){
public int compare(Student s1, Student s2) {
//先排年龄
if(s1.age!=s2.age){
return s1.age-s2.age;
}
else{
//年龄相同则按姓名排序
if(!s1.name.equals(s2.name)){
return s1.name.compareTo(s2.name);
}
else{
//姓名也相同则按学号排序
return s1.id-s2.id;
}
}
}
};
Student stu1 = new Student (1,"zhangsan","male",28,"cs");
Student stu2 = new Student (2,"lisi","female",19,"cs");
Student stu3 = new Student (3,"wangwu","male",22,"cs");
Student stu4 = new Student (4,"zhaoliu","female",17,"cs");
Student stu5 = new Student (5,"jiaoming","male",22,"cs");
ArrayList<Student> List = new ArrayList<Student>();
List.add(stu1);
List.add(stu2);
List.add(stu3);
List.add(stu4);
List.add(stu5);
//这里就会自动根据规则进行排序
Collections.sort(List,comparator);
display(List);
}
static void display(ArrayList<Student> lst){
for(Student s:lst)
System.out.println(s);
}
}
class Student{
int age;
int id;
String gender;
String name;
String cs;
Student(int id,String name,String gender,int age,String cs){
this.age=age;
this.name=name;
this.gender=gender;
this.id=id;
this.cs=cs;
}
public String toString(){
return id+" "+name+" "+gender+" "+age+" "+cs;
}
}
展开全部
修改你的 Student class,添加 Comparable 接口,重写 compareTo 方法。然后你可以用 TreeSet 结构进行排序。它会自动排序。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这只是一个做过的例子,我最近很忙,只能提示你一下,相信你能弄出来的,
方法:重写方法 equals,就是在你定义的类里面加上下面的方法,但内容肯定不同啦。下面是一个例子你看一下就ok了。。。呵呵记得给分啦
。一定记住要重写equals方法,还有类要是想接口 compareable一个并不能少。今天晚了明天我吧完整的我的程序给你吧
方法:重写方法 equals,就是在你定义的类里面加上下面的方法,但内容肯定不同啦。下面是一个例子你看一下就ok了。。。呵呵记得给分啦
。一定记住要重写equals方法,还有类要是想接口 compareable一个并不能少。今天晚了明天我吧完整的我的程序给你吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询