public class Test {
public static void main(String[] args) {
## 定义一个ArrayList集合,保存学生的对象。
## 快速实例化5个学生对象,依次存入集合中。
## 将集合中的对象进行排序,三门课程的总分最高分在前面,总分最低分在后面。
## 请实现,并输出集合数据验证。
## 算出语文的平均分输出。
## 语文分数低于平均分的学生信息输出。(对象里面的输出方法)
## 数学最高分的学生信息输出。
ArrayList<Student> list=new ArrayList<Students>();
Student s1=new Students("张三",81,72,99);
Student s2=new Students("李四",82,72,55);
Student s3=new Students("王五",83,73,46);
Student s4=new Students("赵六",84,77,86);
Student s5=new Students("田七",85,79,77);
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
Iterator<Students> iterator=list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next().PrintStu());
}
Collections.sort(list, new Comparator<Students>() {
@Override
public int compare(Students o1, Students o2) {
int num1=o1.getYwscore()+o1.getYyscore()+o1.getYyscore();
int num2=o2.getYwscore()+o2.getYyscore()+o2.getYyscore();
if (num1>num2){
return -1;
}if (num1==num2){
return 0;
}
return 1;
}
});
System.out.println(list);
}
public class Student {
String name;
int ywscore;
int yyscore;
int sxscore;
public Students(){}
public Students(String name, int ywscore, int yyscore, int sxscore) {
this.name = name;
this.ywscore = ywscore;
this.yyscore = yyscore;
this.sxscore = sxscore;
}
public boolean PrintStu(){
System.out.println("姓名:"+name+":语文成绩:"+ywscore+",英语成绩"+yyscore+",数学成绩:"+sxscore);
return false;
}
public void setName(String name) {
this.name = name;
}
public void setYwscore(int ywscore) {
this.ywscore = ywscore;
}
public void setYyscore(int yyscore) {
this.yyscore = yyscore;
}
public void setSxscore(int sxscore) {
this.sxscore = sxscore;
}
public String getName() {
return name;
}
public int getYwscore() {
return ywscore;
}
public int getYyscore() {
return yyscore;
}
public int getSxscore() {
return sxscore;
}
}