mardi 18 septembre 2018

Having issue with shuffle array that the array return null instead of the random integer in java

private static <T> void shuffle(T[] array){
    if(array==null || array.length < 2){
        return;
    }

    for(int i=1;i<array.length;i++){
        int a = rng.nextInt(i+1);
        System.out.println(a);
        T temp = array[a];
        System.out.println(temp);
        array[a] = array[i];
        array[i] = temp;
    }
}

static boolean checkSorted(ISort sorter, int size) {
    Integer[] data = new Integer[size];
    shuffle(data);
}

The output of printing integer a and T temp: 0 null 2 null 0 null 2 null 0 null 4 null 7 null 8 null 6

I'm not sure why the temp is null instead of integer. Can anyone explain to me? What should i do to modify this code to make it work?




Aucun commentaire:

Enregistrer un commentaire