jeudi 17 novembre 2016

Inserting int into random index in arraylist

New to programming, here's my problem. I want to randomly distribute a fixed amount of "ints" (user input) into random indexes in my arraylist. I take to inputs from user, for example:

User types 5: (thats how many indexes I need in my list)

User types 25: (thats the amount that i have to randomly put in the 5 different indexes).

    Scanner sc = new Scanner(System.in);
    System.out.print("Input N: ");
    int N = sc.nextInt(); // N = 5 from the example above
    System.out.print("Input X:  ");
    int X = sc.nextInt(); // X = 25 from the example above
    ArrayList<Integer> ror = new ArrayList<Integer>(N);

    randomInsert(X, N);

}

public static void randomInsert(int X, ArrayList<Integer> ror) {
    for (int i = 0; i < X; i++)

Im thinking that looping through every X and randomly giving it an index in the array "ror" should be easiest? Im kind of stuck here and really appreciate help!

In the end i want to print the array with X randomly distributed in the indexes. Sorry for bad english!




Aucun commentaire:

Enregistrer un commentaire