The Arraylist array in JAVA sorts the students' grades from high to low

The program uses the Arraylist array and the Collections.sort() method. and class methods

Entity class 

package com.ytzl.第二章.demo5序列化.Weeklysurvey.one;
/**
 * @描述
 * @创建人 Wen
 * @时间 2022年05月15日 14:48
 */
public class test {
    private String name;//姓名
    private int number;//学号
    private int score;//成绩

    public test(String name, int number, int score) {
        this.name = name;
        this.number = number;
        this.score = score;
    }

    public test() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public static void sortStudent(test[] s){
        for(int i=0;i<s.length-1;i++){
            for(int j=0;j<s.length-1-i;j++){
                if(s[j].getScore()<s[j+1].getScore()){
                    test temp=s[j];
                    s[j]=s[j+1];
                    s[j+1]=temp;
                }
            }
        }
    }

    public void show(){
        System.out.println("姓名:"+name+",学号:"+number
                +",成绩:"+score);
    }
}

class Test{
    public static void main(String[] args) {
        test[] s=new test[3];
        s[0]=new test("张三",01,90);
        s[1]=new test("李四",02,89);
        s[2]=new test("王五",03,95);

        test.sortStudent(s);
        for (test stus:s) {
            stus.show();
        }
    }
}

Test class:

package com.ytzl.第二章.demo5序列化.Weeklysurvey.one;

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

public class demo {
    public static void main(String[] args) {
        Student student = new Student("李白",60,111);
        Student student1 = new Student("李黑",70,111);
        Student student2 = new Student("李光",50,111);
        Student student3 = new Student("李暗",80,111);
        ArrayList<Student> list = new ArrayList<>();
        list.add(student);
        list.add(student1);
        list.add(student2);
        list.add(student3);
        ArrayList<Integer> list1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            list1.add(list.get(i).getAchievement());
        }
        Collections.sort(list1);
        System.out.println("姓名    成绩    学号");
        for (int i = list.size()-1; i > -1; i--) {
            for (int j = 0; j < list.size(); j++) {
                if (list1.get(i)==list.get(j).getAchievement()){
                    System.out.println(list.get(j).getName()+"     "+list.get(j).getAchievement()+"    "+list.get(j).getNumber());
                }
            }

        }
    }
}

 

Guess you like

Origin blog.csdn.net/ypf3442354429/article/details/124789526