Java 使用集合时出错。sort()
我想知道为什么在尝试对列表进行排序后会出现错误。 当我尝试对包含“Student”对象的列表进行排序时,会发生错误Java 使用集合时出错。sort(),java,sorting,comparator,comparable,Java,Sorting,Comparator,Comparable,我想知道为什么在尝试对列表进行排序后会出现错误。 当我尝试对包含“Student”对象的列表进行排序时,会发生错误 import java.lang.reflect.Array; 导入java.util.*; 导入java.util.ArrayList; 公共课程{ 公共静态void main(字符串[]args){ List classStudents=new ArrayList(); //在列表中添加4名学生 加上(新学生(“查尔斯”,22岁)); 增加(新学生(“Chris”,25));
import java.lang.reflect.Array;
导入java.util.*;
导入java.util.ArrayList;
公共课程{
公共静态void main(字符串[]args){
List classStudents=new ArrayList();
//在列表中添加4名学生
加上(新学生(“查尔斯”,22岁));
增加(新学生(“Chris”,25));
加上(新学生(“罗伯特”,23));
添加(新学生(“Adam”,21));
//分类和打印
Collections.sort(classStudents);//为什么不起作用?
System.out.println(“classStudent(已排序)-->”
+toString(classStudents.toArray());
}
}
班级学生实施可比较的{
//田地
私有字符串名称;
私人互联网;
//建造师
公立学生(字符串名称,整数年龄){
名称=名称;
年龄=年龄;
}
//覆盖比较
公共int比较(学生与其他学生){
if(this.age==otherStudent.age){
返回0;
}else if(此年龄<其他学生温度){
返回-1;
}否则{
返回1;
}
}
//覆盖toString
公共字符串toString(){
返回this.name+“”+this.age;
}
}
接口{
int比较(学生与其他学生);
字符串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不是有界参数的有效替代品如果要对其进行排序,您的Student
类需要实现java.lang.Comparable
接口。否则,该方法怎么可能知道要排序的标准
这应该是很有帮助的:如果你要排序,你的学生
类需要实现java.lang.Comparable
接口。否则,该方法怎么可能知道要排序的标准
这应该是有帮助的:根据错误消息,它可能不太明显,但如果您查看fill,您会发现此方法(与其他标准API方法一样)需要实现API接口中已定义的实例列表,在本例中是
因此,不要试图创建自己的可比较的接口。请记住,若某个方法需要某个类型,则该类型必须已经在某个地方定义(否则,具有该方法的类将无法编译,因为该方法不能使用某个未知/未定义的类型)
另外,在java.util.Comparable
中定义的比较方法是compareTo
而不是compareTo
(java区分大小写,因此这些方法不被视为相等)
其他问题包括
Student
没有temp
字段,因此
} else if (this.age < otherStudent.temp) {
- 在构造函数中,您需要
this.name
来确定所引用的name
this.name
表示该实例的字段,name
是对传递给构造函数的变量的引用。所以你需要
public Student(String name, int age) {
this.name = name;
this.age = age;
}
根据错误消息,这可能不太明显,但如果您查看fill,您会发现此方法(与其他标准API方法一样)需要实现API接口中已定义的实例列表,在本例中是
因此,不要试图创建自己的可比较的接口。请记住,若某个方法需要某个类型,则该类型必须已经在某个地方定义(否则,具有该方法的类将无法编译,因为该方法不能使用某个未知/未定义的类型)
另外,在java.util.Comparable
中定义的比较方法是compareTo
而不是compareTo
(java区分大小写,因此这些方法不被视为相等)
其他问题包括
Student
没有temp
字段,因此
} else if (this.age < otherStudent.temp) {
- 在构造函数中,您需要
this.name
来确定所引用的name
this.name
表示该实例的字段,name
是对传递给构造函数的变量的引用。所以你需要
public Student(String name, int age) {
this.name = name;
this.age = age;
}
Java定义了自己的可比较接口。删除您的并导入,collections方法使用java方法。java定义了自己的可比较接口。删除您的并导入,collections方法使用java方法。首先,有一个错误,您覆盖了可比较的接口方法“compareTo()”——它应该是小写的“c”,而不是大写的
第二,我不知道你想用“接口可比性”开头的最后一点做什么。放下那部分,你不需要它
可比较的接口是API的一部分
干杯。首先,有一个输入错误,您覆盖了可比较的接口方法“compareTo()”——它应该是小写的“c”,而不是大写的
第二,我不知道你想用“接口可比性”开头的最后一点做什么。放下那部分,你不需要它
可比较的接口是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()));
}
}
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;
}
}
import java.util.ArrayList;
导入java.util.array;
导入java.util.Collections;
导入java.util.List;
公共课程{
公共静态void main(字符串[]args){
System.out.println(“嗨!!!”);
List classStudents=new ArrayList();
//在列表中添加4名学生
加上(新学生(“查尔斯”,22岁));
增加(新学生(“Chris”,25));
加上(新学生(“罗伯特”,23));
classStu
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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()));
}
}
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;
}
}