Java 推断的类型不符合声明的绑定
我遇到了一个以前从未见过的错误,我不太确定如何着手修复它。我目前正在尝试创建一个排序类来比较数组中的gradepoints,如果有帮助的话,我会添加它,我遇到了这个错误Java 推断的类型不符合声明的绑定,java,arrays,error-handling,Java,Arrays,Error Handling,我遇到了一个以前从未见过的错误,我不太确定如何着手修复它。我目前正在尝试创建一个排序类来比较数组中的gradepoints,如果有帮助的话,我会添加它,我遇到了这个错误 required: Course[] found: Course[] reason: inferred type does not conform to declared bound(s) inferred: Course bound(s): Comparable<Course> wh
required: Course[]
found: Course[]
reason: inferred type does not conform to declared bound(s)
inferred: Course
bound(s): Comparable<Course>
where Course is a type-variable:
Course extends Comparable<Course> declared in method<Course>SelectionSort(Course[])
}您似乎还没有初始化数组成员,数组是正常的,因为空指针不在for循环(arr.length)您是否对类
课程使用了那些通用方法,而该类课程不实现Comparable
?这是一个愚蠢的错误。。。但是现在我在第23行的同一个类中遇到了一个空指针异常if(arr[scan].compareTo(arr[min])@Afatmunky如何初始化arr
?@Afatmunky否,SelectionSort
排序已经初始化的数组。因此,它应该在方法之外初始化。例如,Course[]arr=new Course[]{new Course(1,“Bla Bla 101”)、new Course(2,“Foo Bar 110”)}
@Afatmunky顺便说一句,这里似乎不需要泛型。在代码中,Course
是类型参数的名称,而不是类本身。使用以下签名可能是合理的:私有静态无效交换(Course[]arr,int i,int j)
和公共无效选择排序(课程[]arr)
public class Sorting
{
private static <Course> void swap(Course[] arr, int i, int j)
{
if(i != j)
{
Course temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
//Sort method
public <Course extends Comparable<Course>> void SelectionSort(Course[] arr)
{
for(int i = 0; i < arr.length-1; i++)
{
int min = i;
for(int scan = i + 1; scan < arr.length; scan++)
{
if(arr[scan].compareTo(arr[min]) <= 0)
min = scan;
}
swap(arr,i,min);
}
}//end Sorting
}//end class Sorting
public class Test1{
public static void main(String [] args)
{
Course[] courses = new Course[7];
Sorting sort = new Sorting();
courses[0] = new CSInfo("CS1083", "A+", 4);
courses[1] = new CSInfo("CS1073", "A-", 4);
courses[3] = new MathStat("MATH1003", "C+", 3);
courses[4] = new MathStat("MATH1013", "B-", 3);
courses[5] = new CSInfo("CS2053", "B-", 4);
courses[6] = new Breadth("POLS2101", "A-", 3);
courses[0].getPoints();
courses[1].getPoints();
//courses[2].getPoints();
courses[3].getPoints();
courses[4].getPoints();
courses[5].getPoints();
courses[6].getPoints();
courses[0].toString();
sort.SelectionSort(courses);
}