Java selectionSort Generic Class

天涯浪子 提交于 2019-12-12 01:06:28

问题


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

来源:https://stackoverflow.com/questions/23227553/java-selectionsort-generic-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!