当前位置 : 主页 > 编程语言 > java >

Java学习Collections之案例:ArrayList存储学生对象并排序

来源:互联网 收集:自由互联 发布时间:2022-07-04
package com . itheima_108 ; import java . util . ArrayList ; import java . util . Collections ; import java . util . Comparator ; /* 需求:ArrayList存储学生对象,使用Collections对ArrayList进行排序 要求:按年龄从小到大


package com.itheima_108;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/*
需求:ArrayList存储学生对象,使用Collections对ArrayList进行排序
要求:按年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
思路:
1.定义学生类
2.创建ArrayList集合对象
3.创建学生对象
4.把学生添加到集合
5.使用Collections对ArrayList集合排序
6.遍历集合
*/
public class CollectionsDemo02 {
public static void main(String[] args) {
//创建ArrayList集合对象
ArrayList<Student> array = new ArrayList<Student>();

//创建学生对象
Student s1 = new Student("林青霞",30);
Student s2 = new Student("张曼玉",35);
Student s3 = new Student("王祖贤",33);
Student s4 = new Student("柳烟",33);

//把学生添加到集合
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);

//使用Collections对ArrayList集合排序
//sort(list<T> list,Comparator<? super T> c)
Collections.sort(array, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
//按年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
int num = s1.getAge() - s2.getAge();
int num2 = num==0?s1.getName().compareTo(s2.getName()):num;
return num2;
}
});

//遍历集合
for(Student s:array){
System.out.println(s.getName() + ":" + s.getAge());
}

}
}
网友评论