(2)定义一个学生管理类StuMgr,实现如下功能: 1)Generate()方法,根据输入数据(直接在程序中定义字符串,用后面给出的测试数据)生成list学生集合。 2)Sort()方法,采用Collections算法类+匿名类实现按照学生成绩降序排序。 3)Show()方法以迭代方法显示集合中所有学生信息。
时间: 2024-02-15 20:05:01 浏览: 28
好的,我已经明白了你的问题。以下是一个简单的 Java 代码实现,满足你的需求:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Student {
String name;
int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
}
public class StuMgr {
private List<Student> students;
public void Generate() {
students = new ArrayList<>();
students.add(new Student("Tom", 80));
students.add(new Student("Jerry", 90));
students.add(new Student("Mickey", 70));
students.add(new Student("Donald", 85));
}
public void Sort() {
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s2.getScore() - s1.getScore();
}
});
}
public void Show() {
for (Student s : students) {
System.out.println("Name: " + s.getName() + ", Score: " + s.getScore());
}
}
public static void main(String[] args) {
StuMgr stuMgr = new StuMgr();
stuMgr.Generate();
stuMgr.Sort();
stuMgr.Show();
}
}
```
在上面的代码中,我们定义了一个 `Student` 类,包含学生的姓名和成绩属性。然后我们定义了一个 `StuMgr` 类,包含了 `Generate()` 方法,用来生成学生集合,`Sort()` 方法,用来对学生集合按照成绩降序排序,`Show()` 方法,用来以迭代方法显示学生信息。最后我们在 `main()` 方法中调用这些方法,得到了我们想要的结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)