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;
}
}
public class Student {
String name;
int ywscore;
int yyscore;
int sxscore;
public Student() {
}
public Student(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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", ywscore=" + ywscore +
", yyscore=" + yyscore +
", sxscore=" + sxscore +
'}';
}
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<Student>();
Student s1 = new Student("张三", 81, 72, 99);
Student s2 = new Student("李四", 82, 72, 55);
Student s3 = new Student("王五", 83, 73, 46);
Student s4 = new Student("赵六", 84, 77, 86);
Student s5 = new Student("田七", 85, 79, 77);
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
// 将集合中的对象进行排序,三门课程的总分最高分在前面,总分最低分在后面。
orderBySum(list);
// 请实现,并输出集合数据验证。
System.out.println("成绩从高到低:" + list);
// 算出语文的平均分输出。
double sum = 0;
for (Student student : list) {
sum += student.getYwscore();
}
double avg = sum / list.size();
System.out.println("语文平均分:" + avg);
// 语文分数低于平均分的学生信息输出。(对象里面的输出方法)
for (Student student : list) {
if (student.getYwscore() < avg) {
System.out.println("语文分数低于平均分的学生信息:" + student);
}
}
// 数学最高分的学生信息输出。
int max = 0;
int index = 0;
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
if (student.getSxscore() > max) {
max = student.getSxscore();
index = i;
}
}
System.out.println("数学最高分的学生信息:" + list.get(index));
}
private static void orderBySum(ArrayList<Student> list) {
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int num1 = o1.getSxscore() + o1.getYwscore() + o1.getYyscore();
int num2 = o2.getSxscore() + o2.getYwscore() + o2.getYyscore();
return num2 - num1;
}
});
}
}
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();
return num2 - num1;
}
});
num1-num2是升序,num2-num1是降序
使用这种方式就可实现各种排序,当然可以Student实现Comparable,两种方式,根据不同的情况,喜好使用,Comparable具体用法自己查一下