使用 Collections.sort() 时出错
Error when using Collections.sort()
我想知道为什么在尝试对列表进行排序后出现错误。
当我尝试对包含 "Student" 个对象的列表进行排序时发生错误。
import java.lang.reflect.Array;
import java.util.*;
import java.util.ArrayList;
public class mainProgram {
public static void main(String[] args) {
List<Student> classStudents = new ArrayList<Student>();
//Add 4 students to List
classStudents.add(new Student("Charles", 22));
classStudents.add(new Student("Chris", 25));
classStudents.add(new Student("Robert", 23));
classStudents.add(new Student("Adam", 21));
//sort and print
Collections.sort(classStudents); //Why does this not work?
System.out.println("classStudent(Sorted) ---> "
+ Arrays.toString(classStudents.toArray()));
}
}
class Student implements Comparable<Student>{
// fields
private String name;
private int age;
//Constructor
public Student(String name, int age){
name = name;
age = age;
}
//Override compare
public int CompareTo(Student otherStudent){
if(this.age == otherStudent.age){
return 0;
} else if(this.age < otherStudent.temp){
return -1;
} else{
return 1;
}
}
//Override toString
public String toString(){
return this.name + " " + this.age;
}
}
interface Comparable<Student>{
int CompareTo(Student otherStudent);
String toString();
}
错误是:
Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (List<Student>). The inferred type Student is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>
我不明白这个错误是什么意思,也不知道如何解决。非常感谢您的意见。
如果要对其进行排序,您的 Student
class 需要实现 java.lang.Comparable
接口。否则,该方法怎么可能知道根据什么标准进行排序?
这应该会有帮助:Why should a Java class implement comparable?
根据错误消息可能不是那么明显,但是如果您看一下 documentation of Collection.sort
you fill find that this method (like rest of standard API methods) expects list of instances implementing already defined in API interface which in this case is java.lang.Comparable
。
所以不要尝试创建自己的 Comparable
界面。请记住,如果某些方法需要某种类型,则该类型必须已经在某处定义(否则 class 使用此类方法将无法编译,因为方法不能使用某些 unknown/undefined 类型)。
此外,java.util.Comparable
中定义的比较方法是 compareTo
而不是 CompareTo
(Java 区分大小写,因此这些方法不被视为相同)。
其他问题是
Student
没有 temp
字段所以
} else if (this.age < otherStudent.temp) {
应该是
} else if (this.age < otherStudent.age) {
甚至更好,而不是这个 age
比较条件 if(..<..){-1} else if(..==..){0} else {1}
只需使用
return Integer.compare(age, otherStudent.age);
在构造函数中你需要this.name
来确定你指的是哪个name
。 this.name
表示此实例的字段,name
是对传递给构造函数的变量的引用。所以你需要
public Student(String name, int age) {
this.name = name;
this.age = age;
}
Java定义了自己的Comparable接口。删除你的并导入那里,集合方法使用 java 一个。
首先,有一个错字,您覆盖了 Comparable 接口方法,"compareTo()" --- 它应该是小写 "c",而不是大写。
其次,我不知道您要对开始的最后一位做什么,"interface Comparable"。删除那部分 --- 你不需要它。
Comparable 接口是 API 的一部分。
干杯。
做了一些小改动,看看:
MainProgram.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MainProgram {
public static void main(String[] args) {
System.out.println("HI !!!");
List<Student> classStudents = new ArrayList<Student>();
//Add 4 students to List
classStudents.add(new Student("Charles", 22));
classStudents.add(new Student("Chris", 25));
classStudents.add(new Student("Robert", 23));
classStudents.add(new Student("Adam", 21));
//sort and print
Collections.sort(classStudents); //Why does this not work?
System.out.println("classStudent(Sorted) ---> "
+ Arrays.toString(classStudents.toArray()));
}
}
Student.java
class Student implements java.lang.Comparable<Student>{
// fields
private String name;
private int age;
//Constructor
public Student(String name, int age){
this.name = name;
this.age = age;
}
//Override compare
public int compareTo(Student otherStudent){
if(this.age == otherStudent.age){
return 0;
} else if(this.age < otherStudent.age){
return -1;
} else{
return 1;
}
}
//Override toString
public String toString(){
return this.name + " " + this.age;
}
}
输出: classStudent(已排序)---> [Adam 21,Charles 22,Robert 23,Chris 25]
我想知道为什么在尝试对列表进行排序后出现错误。 当我尝试对包含 "Student" 个对象的列表进行排序时发生错误。
import java.lang.reflect.Array;
import java.util.*;
import java.util.ArrayList;
public class mainProgram {
public static void main(String[] args) {
List<Student> classStudents = new ArrayList<Student>();
//Add 4 students to List
classStudents.add(new Student("Charles", 22));
classStudents.add(new Student("Chris", 25));
classStudents.add(new Student("Robert", 23));
classStudents.add(new Student("Adam", 21));
//sort and print
Collections.sort(classStudents); //Why does this not work?
System.out.println("classStudent(Sorted) ---> "
+ Arrays.toString(classStudents.toArray()));
}
}
class Student implements Comparable<Student>{
// fields
private String name;
private int age;
//Constructor
public Student(String name, int age){
name = name;
age = age;
}
//Override compare
public int CompareTo(Student otherStudent){
if(this.age == otherStudent.age){
return 0;
} else if(this.age < otherStudent.temp){
return -1;
} else{
return 1;
}
}
//Override toString
public String toString(){
return this.name + " " + this.age;
}
}
interface Comparable<Student>{
int CompareTo(Student otherStudent);
String toString();
}
错误是:
Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (List<Student>). The inferred type Student is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>
我不明白这个错误是什么意思,也不知道如何解决。非常感谢您的意见。
如果要对其进行排序,您的 Student
class 需要实现 java.lang.Comparable
接口。否则,该方法怎么可能知道根据什么标准进行排序?
这应该会有帮助:Why should a Java class implement comparable?
根据错误消息可能不是那么明显,但是如果您看一下 documentation of Collection.sort
you fill find that this method (like rest of standard API methods) expects list of instances implementing already defined in API interface which in this case is java.lang.Comparable
。
所以不要尝试创建自己的 Comparable
界面。请记住,如果某些方法需要某种类型,则该类型必须已经在某处定义(否则 class 使用此类方法将无法编译,因为方法不能使用某些 unknown/undefined 类型)。
此外,java.util.Comparable
中定义的比较方法是 compareTo
而不是 CompareTo
(Java 区分大小写,因此这些方法不被视为相同)。
其他问题是
Student
没有temp
字段所以} else if (this.age < otherStudent.temp) {
应该是
} else if (this.age < otherStudent.age) {
甚至更好,而不是这个
age
比较条件if(..<..){-1} else if(..==..){0} else {1}
只需使用return Integer.compare(age, otherStudent.age);
在构造函数中你需要
this.name
来确定你指的是哪个name
。this.name
表示此实例的字段,name
是对传递给构造函数的变量的引用。所以你需要public Student(String name, int age) { this.name = name; this.age = age; }
Java定义了自己的Comparable接口。删除你的并导入那里,集合方法使用 java 一个。
首先,有一个错字,您覆盖了 Comparable 接口方法,"compareTo()" --- 它应该是小写 "c",而不是大写。
其次,我不知道您要对开始的最后一位做什么,"interface Comparable"。删除那部分 --- 你不需要它。
Comparable 接口是 API 的一部分。
干杯。
做了一些小改动,看看:
MainProgram.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MainProgram {
public static void main(String[] args) {
System.out.println("HI !!!");
List<Student> classStudents = new ArrayList<Student>();
//Add 4 students to List
classStudents.add(new Student("Charles", 22));
classStudents.add(new Student("Chris", 25));
classStudents.add(new Student("Robert", 23));
classStudents.add(new Student("Adam", 21));
//sort and print
Collections.sort(classStudents); //Why does this not work?
System.out.println("classStudent(Sorted) ---> "
+ Arrays.toString(classStudents.toArray()));
}
}
Student.java
class Student implements java.lang.Comparable<Student>{
// fields
private String name;
private int age;
//Constructor
public Student(String name, int age){
this.name = name;
this.age = age;
}
//Override compare
public int compareTo(Student otherStudent){
if(this.age == otherStudent.age){
return 0;
} else if(this.age < otherStudent.age){
return -1;
} else{
return 1;
}
}
//Override toString
public String toString(){
return this.name + " " + this.age;
}
}
输出: classStudent(已排序)---> [Adam 21,Charles 22,Robert 23,Chris 25]