static void sort(){ //按照成绩排序 int [][]grade = new int[students.size()][2]; for (int i = 0; i < students.size(); i++) { final Student student = students.get(i); grade[i][0] = student.getGrade(); grade[i][1] = i; } for (int i = 0; i < students.size(); i++) { for (int j = 1; j < students.size() - i; j++) { if (grade[j - 1][0] > grade[j][0]){ int a = grade[j][0]; grade[j][0] = grade[j - 1][0]; grade[j - 1][0] = a; int c = grade[j][1]; grade[j][1] = grade[j - 1][1]; grade[j - 1][1] = c; } } }
时间: 2024-04-28 22:26:42 浏览: 70
这段代码是用来对一个学生列表按照成绩进行排序的。它首先创建了一个二维数组 grade,其中第一列存储学生的成绩,第二列存储学生在原列表中的索引。然后使用冒泡排序算法对 grade 数组进行排序,排序时比较的是 grade 数组的第一列(即学生的成绩)。最终,按照排好序的 grade 数组的第二列,重新排列原学生列表。
相关问题
import java.util.List; import java.util.ArrayList; public class paixubiancheng3 { public static void main(String[] args) { StudentClass sClass = new StudentClass(); sClass.createClass(); System.out.println("Original Order:"); System.out.println(sClass.output()); sClass.sort(); System.out.println("Sorted Order:"); System.out.println(sClass.output()); } } class Student{ private String name; private double grade; private int age; public Student(String name,int age,double grade){ this.name = name; this.age = age; this.grade = grade; } public void setname(){ this.name = name; } public void setgrade(){ this.grade = grade; } public void setage(){ this.age = age; } public String getname(){ return name; } public double getgrade(){ return grade; } public int getage(){ return age; } } class StudentClass{ private int size; private List<Student> stuList; public StudentClass(){ size = 0; stuList = null; } public void createClass(){ String names[] = {"Tom","Jerry","Snoopy","Mary","Rose"}; double grades[] = {67,78.5,98,76.5,90}; int ages[] = {17,18,18,19,17}; size = names.length; stuList = new ArrayList<Student>(); Student temp; for (int i = 0; i<size ;i++ ) { temp = new Student(names[i],ages[i],grades[i]); stuList.add(temp); } } public void sort(){ Student temp; for(int i=0;i<size;i++){ for(int j=1;j<size;j++){ if(stuList.get(j-1).getage()>stuList.get(j).getage()){ temp=stuList.get(j-1); stuList.set(j-1,stuList.get(j)); stuList.set(j,temp); } else if (stuList.get(j-1).getage()=stuList.get(j).getage()){ if(stuList.get(j-1).getname().compareTo(stuList.get(j).getname())>0){ temp=stuList.get(j-1); stuList.set(j-1,stuList.get(j)); stuList.set(j,temp); } } else{} } } } public String output(){ StringBuilder studentInfo = new StringBuilder(); for(Student stu : stuList){ studentInfo.append("Age: "+stu.getage()+"\tName: "+stu.getname()+"\r\n"); } studentInfo.append("total: "+size+" students\n"); return studentInfo.toString(); } public void add(Student s){ stuList.add(s); size = stuList.size(); } }这段代码有错吗?如果有请修改
有几处错误,以下是修改后的代码:
```
import java.util.List;
import java.util.ArrayList;
public class PaixuBiancheng3 {
public static void main(String[] args) {
StudentClass sClass = new StudentClass();
sClass.createClass();
System.out.println("Original Order:");
System.out.println(sClass.output());
sClass.sort();
System.out.println("Sorted Order:");
System.out.println(sClass.output());
}
}
class Student {
private String name;
private double grade;
private int age;
public Student(String name, int age, double grade){
this.name = name;
this.age = age;
this.grade = grade;
}
public void setName(String name){
this.name = name;
}
public void setGrade(double grade){
this.grade = grade;
}
public void setAge(int age){
this.age = age;
}
public String getName(){
return name;
}
public double getGrade(){
return grade;
}
public int getAge(){
return age;
}
}
class StudentClass {
private int size;
private List<Student> stuList;
public StudentClass(){
size = 0;
stuList = new ArrayList<Student>();
}
public void createClass(){
String names[] = {"Tom","Jerry","Snoopy","Mary","Rose"};
double grades[] = {67,78.5,98,76.5,90};
int ages[] = {17,18,18,19,17};
size = names.length;
Student temp;
for (int i = 0; i<size ;i++ ) {
temp = new Student(names[i],ages[i],grades[i]);
stuList.add(temp);
}
}
public void sort(){
Student temp;
for(int i=0;i<size;i++){
for(int j=1;j<size-i;j++){
if(stuList.get(j-1).getAge()>stuList.get(j).getAge()){
temp=stuList.get(j-1);
stuList.set(j-1,stuList.get(j));
stuList.set(j,temp);
} else if (stuList.get(j-1).getAge()==stuList.get(j).getAge()
&& stuList.get(j-1).getName().compareTo(stuList.get(j).getName())>0){
temp=stuList.get(j-1);
stuList.set(j-1,stuList.get(j));
stuList.set(j,temp);
}
}
}
}
public String output(){
StringBuilder studentInfo = new StringBuilder();
for(Student stu : stuList){
studentInfo.append("Age: "+stu.getAge()+"\tName: "+stu.getName()+"\r\n");
}
studentInfo.append("total: "+size+" students\n");
return studentInfo.toString();
}
public void add(Student s){
stuList.add(s);
size = stuList.size();
}
}
```
主要的修改有:
1. 类名 PaixuBiancheng3 首字母应该大写。
2. Student 类中的 setter 方法没有传入参数。
3. StudentClass 类中创建 List 对象时没有初始化,应该改为 `stuList = new ArrayList<Student>();`。
4. sort 方法中的比较条件有误,应该使用 `>` 或 `<` 进行比较。
5. sort 方法中比较年龄相同的情况下,应该再比较姓名的字典序。
4. Collections (1)改写Student类,增加int grade属性。 (2)随机生成100个Student对象放入students数组。 (3)将students数组转换成studentList(List类型)。 (4)使用Collections.sort对student的成绩进行排序。 (5)使用Collections的min和max方法找到成绩最高的和成绩最低学生。 (6)使用Collections的binarySearch查找成绩等于60的学生。 (7)编写方法从studentList找出grade小于60的学生,放入otherList。 (8)使用List的removeAll方法,从studentList中移除otherList。 (9)使用Collections的shuffle方法,打乱studentList中学生排列的顺序。 (10)将studentList转换为数组。
1. 改写Student类,增加int grade属性。
```java
public class Student {
private String name;
private int age;
private int grade;
public Student(String name, int age, int grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getGrade() {
return grade;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", grade=" + grade +
'}';
}
}
```
2. 随机生成100个Student对象放入students数组。
```java
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random();
Student[] students = new Student[100];
for (int i = 0; i < students.length; i++) {
students[i] = new Student("Student" + i, random.nextInt(10) + 15, random.nextInt(101));
}
}
}
```
3. 将students数组转换成studentList(List类型)。
```java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Random random = new Random();
Student[] students = new Student[100];
for (int i = 0; i < students.length; i++) {
students[i] = new Student("Student" + i, random.nextInt(10) + 15, random.nextInt(101));
}
List<Student> studentList = new ArrayList<>();
for (Student student : students) {
studentList.add(student);
}
}
}
```
4. 使用Collections.sort对student的成绩进行排序。
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random();
Student[] students = new Student[100];
for (int i = 0; i < students.length; i++) {
students[i] = new Student("Student" + i, random.nextInt(10) + 15, random.nextInt(101));
}
List<Student> studentList = new ArrayList<>();
for (Student student : students) {
studentList.add(student);
}
Collections.sort(studentList, (s1, s2) -> s1.getGrade() - s2.getGrade());
}
}
```
5. 使用Collections的min和max方法找到成绩最高的和成绩最低学生。
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random();
Student[] students = new Student[100];
for (int i = 0; i < students.length; i++) {
students[i] = new Student("Student" + i, random.nextInt(10) + 15, random.nextInt(101));
}
List<Student> studentList = new ArrayList<>();
for (Student student : students) {
studentList.add(student);
}
Collections.sort(studentList, (s1, s2) -> s1.getGrade() - s2.getGrade());
System.out.println("最高成绩学生:" + Collections.max(studentList, (s1, s2) -> s1.getGrade() - s2.getGrade()));
System.out.println("最低成绩学生:" + Collections.min(studentList, (s1, s2) -> s1.getGrade() - s2.getGrade()));
}
}
```
6. 使用Collections的binarySearch查找成绩等于60的学生。
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random();
Student[] students = new Student[100];
for (int i = 0; i < students.length; i++) {
students[i] = new Student("Student" + i, random.nextInt(10) + 15, random.nextInt(101));
}
List<Student> studentList = new ArrayList<>();
for (Student student : students) {
studentList.add(student);
}
Collections.sort(studentList, (s1, s2) -> s1.getGrade() - s2.getGrade());
int index = Collections.binarySearch(studentList, new Student("", 0, 60), (s1, s2) -> s1.getGrade() - s2.getGrade());
if (index >= 0) {
System.out.println("成绩等于60的学生:" + studentList.get(index));
} else {
System.out.println("没有成绩等于60的学生");
}
}
}
```
7. 编写方法从studentList找出grade小于60的学生,放入otherList。
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random();
Student[] students = new Student[100];
for (int i = 0; i < students.length; i++) {
students[i] = new Student("Student" + i, random.nextInt(10) + 15, random.nextInt(101));
}
List<Student> studentList = new ArrayList<>();
for (Student student : students) {
studentList.add(student);
}
Collections.sort(studentList, (s1, s2) -> s1.getGrade() - s2.getGrade());
List<Student> otherList = new ArrayList<>();
for (Student student : studentList) {
if (student.getGrade() < 60) {
otherList.add(student);
}
}
}
}
```
8. 使用List的removeAll方法,从studentList中移除otherList。
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random();
Student[] students = new Student[100];
for (int i = 0; i < students.length; i++) {
students[i] = new Student("Student" + i, random.nextInt(10) + 15, random.nextInt(101));
}
List<Student> studentList = new ArrayList<>();
for (Student student : students) {
studentList.add(student);
}
Collections.sort(studentList, (s1, s2) -> s1.getGrade() - s2.getGrade());
List<Student> otherList = new ArrayList<>();
for (Student student : studentList) {
if (student.getGrade() < 60) {
otherList.add(student);
}
}
studentList.removeAll(otherList);
}
}
```
9. 使用Collections的shuffle方法,打乱studentList中学生排列的顺序。
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random();
Student[] students = new Student[100];
for (int i = 0; i < students.length; i++) {
students[i] = new Student("Student" + i, random.nextInt(10) + 15, random.nextInt(101));
}
List<Student> studentList = new ArrayList<>();
for (Student student : students) {
studentList.add(student);
}
Collections.sort(studentList, (s1, s2) -> s1.getGrade() - s2.getGrade());
List<Student> otherList = new ArrayList<>();
for (Student student : studentList) {
if (student.getGrade() < 60) {
otherList.add(student);
}
}
studentList.removeAll(otherList);
Collections.shuffle(studentList);
}
}
```
10. 将studentList转换为数组。
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random();
Student[] students = new Student[100];
for (int i = 0; i < students.length; i++) {
students[i] = new Student("Student" + i, random.nextInt(10) + 15, random.nextInt(101));
}
List<Student> studentList = new ArrayList<>();
for (Student student : students) {
studentList.add(student);
}
Collections.sort(studentList, (s1, s2) -> s1.getGrade() - s2.getGrade());
List<Student> otherList = new ArrayList<>();
for (Student student : studentList) {
if (student.getGrade() < 60) {
otherList.add(student);
}
}
studentList.removeAll(otherList);
Collections.shuffle(studentList);
Student[] studentArray = studentList.toArray(new Student[0]);
}
}
```
阅读全文