-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaggregation15.java
More file actions
75 lines (59 loc) · 1.79 KB
/
aggregation15.java
File metadata and controls
75 lines (59 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* @由于个人水平有限, 难免有些错误, 还请指点:
* @Author: cpu_code
* @Date: 2020-09-17 13:02:09
* @LastEditTime: 2020-09-17 13:10:59
* @FilePath: \java\aggregation\aggregation15.java
* @Gitee: [https://gitee.com/cpu_code](https://gitee.com/cpu_code)
* @Github: [https://github.com/CPU-Code](https://github.com/CPU-Code)
* @CSDN: [https://blog.csdn.net/qq_44226094](https://blog.csdn.net/qq_44226094)
* @Gitbook: [https://923992029.gitbook.io/cpucode/](https://923992029.gitbook.io/cpucode/)
*/
package aggregation;
import java.util.ArrayList;
import java.util.Collections;
public class aggregation15 {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("cpucode", 22));
list.add(new Student("cpu", 21));
list.add(new Student("hello", 11));
/*
让学生 按照年龄排序 升序
*/
Collections.sort(list);//要求 该list中元素类型 必须实现比较器Comparable接口
for (Student student : list) {
System.out.println(student);
}
}
}
class Student implements Comparable<Student>{
private String name;
private int age;
public Student() {
}
public Student(String name, int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "String {" + "name=" + name + '\'' + ", age=" + age + '}';
}
@Override
public int compareTo(Student o) {
return this.age - o.age;
}
}