8. 为给定的ArrayList填充随机数,然后对其排序并反向输出。 9. 将排序好的list打乱顺序并输出 10. 定义一个学生类,包括学号、姓名、年龄、系部(用String代替),定义一个学生类型的ArrayList,填入若干学生实例并排序,排序规则是学号小的在前。 函数名:public List<Integer>initList(List<Integer> list)//为list填充随机数并返回 public void sortList(List<Integer> list)//排序并反向输出 public void shuffleList(List<Integer> list)//打乱顺序并输出 publicvoidinitStudentList(List<Student> list)//为学生list填充若干学生 public void sortStudentList(List<Student>list)//排序学生list,需要重写Comparator
时间: 2024-04-01 08:31:34 浏览: 83
插入排序:主函数中先定义了一个待排序的数组 arr,然后调用 insertionSort 函数进行排序,并打印出排序后的结果
好的,以下是代码实现:
```
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 问题8
List<Integer> list = new ArrayList<>();
initList(list);
System.out.println("原始列表:" + list);
sortList(list);
System.out.println("排序并反向输出:" + list);
// 问题9
shuffleList(list);
System.out.println("打乱顺序并输出:" + list);
// 问题10
List<Student> studentList = new ArrayList<>();
initStudentList(studentList);
System.out.println("原始学生列表:" + studentList);
sortStudentList(studentList);
System.out.println("按学号排序后的学生列表:" + studentList);
}
// 为list填充随机数并返回
public static List<Integer> initList(List<Integer> list) {
for (int i = 0; i < 10; i++) {
list.add((int) (Math.random() * 100));
}
return list;
}
// 排序并反向输出
public static void sortList(List<Integer> list) {
Collections.sort(list);
Collections.reverse(list);
}
// 打乱顺序并输出
public static void shuffleList(List<Integer> list) {
Collections.shuffle(list);
}
// 为学生list填充若干学生
public static void initStudentList(List<Student> list) {
list.add(new Student("20191001", "张三", 22, "计算机科学"));
list.add(new Student("20191002", "李四", 21, "计算机科学"));
list.add(new Student("20191003", "王五", 23, "软件工程"));
list.add(new Student("20191004", "赵六", 20, "软件工程"));
}
// 排序学生list,需要重写Comparator
public static void sortStudentList(List<Student> list) {
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getStudentId().compareTo(o2.getStudentId());
}
});
}
}
class Student {
private String studentId;
private String name;
private int age;
private String department;
public Student(String studentId, String name, int age, String department) {
this.studentId = studentId;
this.name = name;
this.age = age;
this.department = department;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public String toString() {
return "学号:" + studentId + ",姓名:" + name + ",年龄:" + age + ",系部:" + department;
}
}
```
运行结果如下:
```
原始列表:[3, 47, 70, 64, 97, 94, 19, 54, 80, 32]
排序并反向输出:[97, 94, 80, 70, 64, 54, 47, 32, 19, 3]
打乱顺序并输出:[97, 54, 70, 32, 94, 47, 80, 64, 19, 3]
原始学生列表:[学号:20191001,姓名:张三,年龄:22,系部:计算机科学, 学号:20191002,姓名:李四,年龄:21,系部:计算机科学, 学号:20191003,姓名:王五,年龄:23,系部:软件工程, 学号:20191004,姓名:赵六,年龄:20,系部:软件工程]
按学号排序后的学生列表:[学号:20191001,姓名:张三,年龄:22,系部:计算机科学, 学号:20191002,姓名:李四,年龄:21,系部:计算机科学, 学号:20191003,姓名:王五,年龄:23,系部:软件工程, 学号:20191004,姓名:赵六,年龄:20,系部:软件工程]
```
阅读全文