java定义一个用于存放学生对象的一维数组StudentInfo要求数组中的学生对象按成绩升序排列并输出数组元素

2024-11-27 17:36:45
推荐回答(2个)
回答1:

闲着无聊 送你了
package test;

public class Student {
private String name;
private int age;
public Student(String name,int age){
this.name=name;
this.age=age;
}
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;
}
}

package test;

import java.util.Arrays;

public class Main {
public static void main(String[] args) {

Student [] studentInfo=new Student[5];
studentInfo[0]=new Student("张", 10);
studentInfo[1]=new Student("张", 9);
studentInfo[2]=new Student("李", 3);
studentInfo[3]=new Student("王1", 7);
studentInfo[4]=new Student("王2", 88);
Student [] newStudentInfo=Main.selectSort(studentInfo);
for(int i=0;i System.out.println(newStudentInfo[i].getName()+":"+newStudentInfo[i].getAge());
}
}
public static Student [] selectSort(Student [] studentInfo){//选择排序算法
for (int i=0;i int min=i;
for (int j=i+1;j if (studentInfo[min].getAge()>studentInfo[j].getAge()){
min=j;
}
}
if (min!=i){
Student temp=studentInfo[i];
studentInfo[i]=studentInfo[min];
studentInfo[min]=temp;
}
}
return studentInfo;
}
}

回答2:

6cqcwd的产物事儿