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转换为数组。
时间: 2023-06-24 17:03:50 浏览: 96
1_1. 产生100个随机数_求其最小值和最大值以及平均值_
5星 · 资源好评率100%
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]);
}
}
```
阅读全文