推断类型不符合声明的边界

时间:2015-04-16 22:58:19

标签: java arrays error-handling

我遇到了一个我以前没见过的错误,我不太确定如何修复它。我目前正在尝试创建一个排序类来比较数组中的成绩点,我将添加它,如果它会有所帮助,我遇到了这个错误。

  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[])

产生此错误的类:

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);

    }

}

1 个答案:

答案 0 :(得分:1)

似乎你没有初始化数组成员,数组没问题,因为null指针不在for循环中(arr.length)