1 题目
2 代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
public class A1083 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
ArrayList<StudentGrade> studentGrades = new ArrayList<>();
for (int i = 0; i < N; i++) {
String[] input = br.readLine().split(" ");
studentGrades.add(new StudentGrade(input[0], input[1], Integer.parseInt(input[2])));
}
String[] line = br.readLine().split(" ");
int grade1 = Integer.parseInt(line[0]);
int grade2 = Integer.parseInt(line[1]);
ArrayList<StudentGrade> student = new ArrayList<>();
for (StudentGrade s : studentGrades) {
if (s.getGrade() >= grade1 && s.getGrade() <= grade2)
student.add(s);
}
if (student.size() == 0)
System.out.println("NONE");
else {
Collections.sort(student);
for (StudentGrade s : student) {
System.out.println(s.getName() + " " + s.getId());
}
}
}
}
class StudentGrade implements Comparable<StudentGrade> {
private String name;
private String id;
private int grade;
public StudentGrade(String name, String id, int grade) {
this.name = name;
this.id = id;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
@Override
public int compareTo(StudentGrade o) {
return o.getGrade() - this.grade;
}
}
3 要点
(1)List的第二种排序方式,实现Comparable<>接口。