package ahaAlgorithm.chapter1; import java.util.Scanner; /** * * @Title: BubbleSort1.java * @Package ahaAlgorithm.chapter1 * @Description: TODO(类在排序中的应用) * @author YoungSone * @date 2020年7月29日 上午10:03:09 * @version V1.0 */ public class BubbleSort1 { private static Scanner in; public static void main(String[] args) { Student t = new Student(); int i, j, n, score; String name; in = new Scanner(System.in); System.out.println("有多少人参加?"); n = in.nextInt(); Student[] student = new Student[n];//定义Student类数组 for (i = 0; i < n; i++) { student[i]=new Student(); //new Student类 student[i].setName(in.next()); student[i].setScore(in.nextInt()); } for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1 - i; j++) { if (student[j].getScore() > student[j + 1].getScore()) { t = student[j]; student[j] = student[j + 1]; student[j + 1] = t; } } } for (Student s : student) { System.out.println(s.getName() + " " + s.getScore()); } } } class Student{ private String name; private int score; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } }
java中关于类数组要进行两次new定义
Student student =new Student[n] //声明student数组
sutdent[i]=new student()//声明 student(i)为一个新的类student
第一次定义:
student[1] | student[n] |
第二次定义
student[1] student[n]
student类 变量 score 和 name |
student类 |