-1

So I'm supposed to create a Permutation list that looks like this

Output:

Final ouput

and I can't seem to get my code to repeat 10 times (Edit: it tells me that numbers.add(og.get(randPick)); ` is out of bounds because it becomes a negative number

CODE:

import java.util.ArrayList;
import java.util.Random;
public class ArrayListTest
{


    public void Arrays(){
        ArrayList<Integer> og = new ArrayList<Integer>();

        Random random = new Random();
        //what is inside the Array (Digits 1-10) 
        for (int x = 1; x <= 10; x++) {
            og.add(x);   
        }

        ArrayList<Integer> numbers = new ArrayList<Integer>();
        //printing 
        int a = 1; 
        while(a < 10){
            System.out.print("List " + a + ":");
            a++;
            for(int y = 0; y < 10; y++){
                //the random number itself 
                int randPick = random.nextInt(og.size());

                //adding the new random number to the permutation
                numbers.add(og.get(randPick));

                //taking out the random number slot chosen 
                og.remove(randPick);

                //printing out the current numbers in one line
                System.out.print(numbers.get(y) + " ");
            }
        }
    }
}
John Yu
  • 13
  • 2
  • 1
    What exactly is the problem? – Idos Jan 28 '16 at 08:32
  • @bmarkham, this description have no useful meaning in coding terms. OP should add an exact description of what happens, is it an exception? Something does print? What prints? – amotzg Jan 28 '16 at 08:39
  • Your problem lies in the `og.remove(randPick)`. This changes the list-size from 10 to 9, so in one of the next iterations of the while-loop, the `randomPick` (which is the index) could be 9 for example, while your list size is only 8, causing an exception. Also, as others have stated, you should have added what the problem was (including the stacktrace), and not let others run the code for themselves. `"I can't seem to get my code to repeat 10 times"`. Ok, but WHY? What is the *specific* problem? (To answer that in this case: the exception + stacktrace). – Kevin Cruijssen Jan 28 '16 at 08:42
  • 1
    @KevinCruijssen, 1) the remove call is in a nesting for loop, it will empty the list on the first iteration of the while loop. 2) randomPick will never give an index out of bounds. It is bounded by og.size(). – amotzg Jan 28 '16 at 08:49
  • 1
    Updated the post! Thank you for the advice, sorry for the inconvinience, the problem is that I can't do another loop with a new list... it continues the original finished loop until it becomes a negative – John Yu Jan 28 '16 at 09:09
  • @amotzg Ah, you're completely right. For 1, there should either be a clone or a copy-instance of the list in each iteration. And for 2 it's just misread on my part. The `og.size()` for the randomIndex is inside the while, so with the remove it indeed changes as well. I personally would have had a completely different approach (probably using a list to fill with the indexes I already covered, so the `.remove()` isn't needed), but OP's method isn't too bad either, apart from the problem this question was made for. – Kevin Cruijssen Jan 28 '16 at 12:06

1 Answers1

2

og.remove(randPick); Will clear the original list on the first while iteration. On the next iteration you will probably get an exception.

Also, see this question for an out of the box solution:

java.util.Collections.shuffle(List);
Community
  • 1
  • 1
amotzg
  • 1,142
  • 12
  • 28