jeudi 8 décembre 2016

Why is there an element that is not being displayed?

I am practicing creating a randomly generating integers in an array, then randomizing the elements in the array. All is well when I print the numbers, but there seems to be one element that does not print when I am displaying the randomized elements. Is there a step I am leaving out?

public class shufflingArrays {
public static void main(String[] args) {


    int[] myList = new int[10];
    System.out.println("Numbers:");
    for(int i = 0; i < myList.length; i++) {
        myList[i] = (int)(Math.random() * 100);
        System.out.print(myList[i] + " ");
    }
    System.out.println("\nRandomized:");

    for (int i = myList.length - 1; i > 0; i--){
        //Generate index j randomly with 0 <= j <= i
        int j = (int)(Math.random() * (i + 1));

        //Swap myList[i]; with myList[j]
        int temp = myList[i];
        myList[i] = myList[j];
        myList[j] = temp;
        System.out.print(myList[i] + " ");
    }   
}




Aucun commentaire:

Enregistrer un commentaire