package day10; public class ObjectArrayTest1 { public static void main(String[] args){ // 创建一个数组对象 里面有3个对象 Student[] arr = new Student[3]; arr[0] = new Student(); arr[0].name = "华华"; arr[0].score = 99.5; arr[1] = new Student(); arr[1].name = "骨头"; arr[1].score = 92.5; arr[2] = new Student(); arr[2].name = "花头"; arr[2].score = 89.5; // 根据分数对数组对象进行排序 for(int i = 0; i < arr.length;i++){ for(int j = 0; j < arr.length - 1;j++){ if(arr[j].score > arr[j+1].score){ Student temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } for(int i = 0; i < arr.length;i++){ System.out.println(arr[i].getInfo()); } } } class Student{ String name; double score; public String getInfo(){ return "姓名:" + name + ";分数是:" + score; }
01-10
1203
08-28
4208