-->

Java selectionSort Generic Class

2019-09-17 20:26发布

问题:

I am trying to program an array sorter, so far this is what i have. When i run it i get array out of bounds exception. I dont know where the array is out of bounds. can someone help me figure out where?

public static void main(String[] args) {

    Integer[] list50k = new Integer[50000];

    // Create random array of 50k
    for (int i = 0; i < 50000; i++){
        Integer x = random.nextInt(9) + 1; 
        list50k[i] = x;
    }
    selectionSort(list50k);

public static <T extends Comparable<T>> void selectionSort(T[] list){
    for(int i=0; i<list.length -1; i++){
        int iSmallest = i;

        for(int j=i+1; j<list.length; j++){
            if(list[iSmallest].compareTo((list[j])) > 0  ){
                iSmallest = j;
            }
        }
        T iSwap = list[iSmallest];
        list[iSmallest] = list[i];
        list[i] = iSwap; 
    }   
}