# Help removing items from an Arraylist and placing into a 2D Array

• 08-17-2011, 01:15 AM
david522
Help removing items from an Arraylist and placing into a 2D Array
I need to remove 52 RANDOM elements from my arraylist and place them into a 2D array with 6 rows and 9 columns. This is my code so far.

Code:

```private Shoe[][] getRandomShoesArray() {                 ArrayList<Shoe> shoes = createTheShoes();                 Shoe [][] randomShoes;                 randomShoes = new Shoe[6][9];                 for(int row=0; row < NUMBER_OF_ROWS; row++) {                         for(int col=0; col < NUMBER_OF_COLS; col++) {                                 for(int i=0;i<shoes.size();i++) {                                         randomShoes[row][col] = shoes.get(i);                                 }                                }                        }                                return null;         }```
I don't think is correct as I only want 52 random shoes but I can't think of which way the loops should go. Also how would I RANDOMLY remove them, Math.random() is the only random method we have learnt. Also in the last 4 rows there would be four blank spaces that can be filled up with null but am not sure how to do this...

Any help appreciated thanks! :D:
• 08-17-2011, 01:30 AM
Junky
Quote:

Originally Posted by david522
I don't think is correct as I only want 52 random shoes

Then I would probably use a loop that iterates 52 times.
Quote:

but I can't think of which way the loops should go.
In that case step away from the computer, grab a pen and some paper and work out how you would do it by hand.
Quote:

Also how would I RANDOMLY remove them,
Quote:

Math.random() is the only random method we have learnt.
You can use Math.random but since that returns a floating point number between 0 and 1 you need to scale it up to 0 - 52. Or you can learn how to use the Random class.
Quote:

Also in the last 4 rows there would be four blank spaces that can be filled up with null but am not sure how to do this...
The default value in an object array is null. If you do not add anything to the array at a certain location then it will be null. You don't need to do anything.
• 08-17-2011, 02:17 AM
Willriker
I am just a novice as well, but i think that your on the correct track with the random idea. I would just be worried about having random 0-52 come up the same number twice. Youll need some way to store the values generated by random, compare new randoms to the stored values before placing the number into the new array so that there are no repeats.

at least that is how i would approach it.
• 08-17-2011, 02:29 AM
Junky
Quote:

Originally Posted by Junky
You can use Math.random but since that returns a floating point number between 0 and 1 you need to scale it up to 0 - 52.

I just realised that should be 0 - size of the list. Which leads to the question what happens if there isn't 52 objects in the List?
• 08-17-2011, 03:31 AM
david522
The number is set as 52 by a final int constant.
I've been thinking of how to store the number that has already been used but am having difficulty seeing how i could be compared to itself...
I was thinking of trying this
Code:

```for(int row=0; row < NUMBER_OF_ROWS; row++) {                         for(int col=0; col < NUMBER_OF_COLS; col++) {                                 for(int i=((int)(Math.random() * TOTAL_NUMBER_OF_SHOES));i<=TOTAL_NUMBER_OF_SHOES;i++) {                                         int stored = i;                                         if(i = i){                                                 .........                                         }                                         randomShoes[row][col] = pack.get(i);                                 }                                }                        }```
I still do not understand how I would go about looping it through 52 times while looping the rows/columns as a true 2D array must have 2 loops?
• 08-17-2011, 04:24 AM
Junky
I still do not fully understand what you are doing. If all you want to do is take 52 Objects from the List and put them into a 2D array then you only need a single loop.
Code:

```loop 52 times {     generate random number n;     get Shoe from List at index n;     add Shoe object to 2D array at position [x][y];     update values for x & y (if needed); }```
Working out what values x and y should be is the only ticky part.
• 08-17-2011, 08:25 AM
david522
Ok so I'm trying to follow your structure but I'm not too sure if this is correct. If you could give me a few further hints that would be great. Really appreciate all your help!

Also I need someway of storing the values I have already used but am figuring that out now... EDIT : I think I just have to remove the elemant from the array list as I use it.

Code:

```private Shoe[][] getRandomShoesArray() {                 ArrayList<Shoe> shoe = createTheShoe();                 Shoe [][] randomShoe;                 randomShoes = new Shoe[6][9];                 int x = 0;                 int y = 0;                 for(int i=0; i <= TOTAL_NUMBER_OF_SHOES; i++) {                         int random = (int)(Math.random() * TOTAL_NUMBER_OF_SHOES);                         Shoe placement = pack.get(random);                         randomShoes[x][y] = placement;                         if (x < 6) {                           x++                         }                       if (y < 9) {                           y++                       }                 }                 return null;         }```
• 08-18-2011, 12:34 AM
Junky
if you were to hard code accessing the elements of a 2D array then it would be:
Code:

```arr[0][0] arr[0][1] arr[0][2] arr[0][3] arr[0][4] arr[0][5] arr[0][6] arr[0][7] arr[0][8] arr[1][0] arr[1][1] arr[1][2]```
Can you see a pattern?
• 08-18-2011, 01:51 AM
david522
Ah cheers, thanks for pointing out the basics to me! Have been playing around with it for hours but I'm fairly certain I'm on the right track now.
Code:

```private Shoe[][] getRandomShoesArray() {                 ArrayList<Shoe> shoe = createTheShoe();                 Shoe [][] randomShoes;                 int x = 0;         int y = 0;                 randomShoes = new Shoe[6][9];                 for (int i = 0; i < TOTAL_NUMBER_OF_SHOES; i++) {                                 int random = ((int)Math.random() * shoe.size());                                 Shoe placement = shoe.get(random);                                 shoe.remove(random);                                 randomShoes[x][y] = placement;                                 if (y == 8 && x < 5) {                                         x++;                                         y = 0;                                 }                                 if (y < 8 && x < 5) {                                         y++;                                                                }                                }                                                                return randomShoes;         }```