题目
代码
package lesson.l10_oop;
public class Student {
int number;
int state;
int score;
public static final int NUM = 20;
public static void main(String[] args) {
Student[] students = new Student[NUM];
for (int i = 0; i < NUM; i++) {
students[i] = new Student();
students[i].number = i + 1;
students[i].state = (int) (Math.random() * 9) + 1;
students[i].score = (int) (Math.random() * 99);
}
Student student = new Student();
student.searchState(students, 3);
student.bubbleSort(students);
student.print(students);
}
public void searchState(Student[] students, int state) {
System.out.println(state + "年纪的学生信息:");
for (Student s :
students) {
if (s.state == state) {
System.out.print(s.toString() + "\n");
}
}
}
public void print(Student[] students) {
for (Student s :
students) {
System.out.println(s.toString());
}
}
public void bubbleSort(Student[] students) {
System.out.println("冒泡排序:");
for (int i = 0; i < students.length - 1; i++) {
for (int j = 0; j < students.length - 1 - i; j++) {
if (students[j].score > students[j + 1].score) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
public void exchange(Student a, Student b) {
a.number = a.number ^ b.number;
b.number = a.number ^ b.number;
a.number = a.number ^ b.number;
a.state = a.state ^ b.state;
b.state = a.state ^ b.state;
a.state = a.state ^ b.state;
a.score = a.score ^ b.score;
b.score = a.score ^ b.score;
a.score = a.score ^ b.score;
}
@Override
public String toString() {
return "Student{" +
"number=" + number +
", state=" + state +
", score=" + score +
'}';
}
}
内存解析