[Java] 纯文本查看 复制代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Demo2 {
public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<Student>();
list.add(new Student("学生一",44,'男'));
list.add(new Student("学生二",15,'男'));
list.add(new Student("学生三",19,'女'));
list.add(new Student("学生四",25,'女'));
list.add(new Student("学生五",63,'男'));
list.add(new Student("学生六",66,'女'));
System.out.println("姓名 "+"年龄 "+"性别");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).getName()+" "+list.get(i).getAge()+" "+list.get(i).getGender());
}
Collections.sort(list, new Comparator<Student>(){
public int compare(Student s1, Student s2) {
return s1.getAge()-s2.getAge();
}
});
list.get(list.size()-1).setName("葫芦娃");
System.out.println("----------------------");
System.out.println("姓名 "+"年龄 "+"性别");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).getName()+" "+list.get(i).getAge()+" "+list.get(i).getGender());
}
}
}
class Student{
private String name;
private int age;
private char gender;
public Student() {
super();
}
public Student(String name, int age, char gender) {
super();
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
}
|