# 2D Array Random Shuffle

• 11-23-2008, 01:57 AM
Nuluvius
2D Array Random Shuffle
Hey,

I would like to ask for some help concerning a game I am currently developing. Im pritty much there with all of the functionality however one thing is bothering me... I need to code a function that will randomly shuffle a 2 dimentional array of pre existing objects(classes). I can do a 1D array fine .. but I dont know where to start for a 2D, could anyone offer me any help on this?

The objects are drawn to an applet therefore they do indeed contain x, y possition information.

Thank you in advance for any assistance you can offer.
• 11-23-2008, 02:01 AM
Nuluvius
oh lets say for agruments sake that the array will only ever be:

int arraySize = >whatever integer<

Array[][] myArray = new Array[arraySize][arraySize]

square array....

and the fuction contained in:

public void Shuffle()
{
first for loop over rows
second (nested) for loop over columns
int r = (int)(Math.random() * arraySize) ??
<< array sorting code >>
}

sorry for not including that info.
• 11-23-2008, 02:09 AM
Fubarable
why not change it to a 1-D array or list, shuffle it, then lay it out in 2-D after the shuffle?
• 11-23-2008, 02:17 AM
Nuluvius
Thank you for your quick answer, I can see that changing it to a 1D array then back would solve it nicely. However I am not sure how to go about doing that.. unfortunatly... would I be able to get a hint?

• 11-23-2008, 03:17 AM
Fubarable
Myself, I'd keep the data in an ArrayList. Then only when I needed to display it or manipulate it in an array, I'd use two nested for loops and place the data into each array slot. There's really not much to it.

For instance, say I want a 2-D array of objects of a type called "MyType":
Code:

```class MyType {   private int value;   public MyType(int value)   {     this.value = value;   }   public int getValue()   {     return value;   }     @Override   public String toString()   {     return String.valueOf(value);   }   }```
I'd do it like so:
Code:

```import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Fubar {   private static final int ARRAY_SIZE = 5;   private MyType[][] myArray = new MyType[ARRAY_SIZE][ARRAY_SIZE];   private List<MyType> myList = new ArrayList<MyType>();     public Fubar()   {     init();   }   public void init()   {     // add ARRAY_SIZE squared elements into the list:     for (int i = 0; i < ARRAY_SIZE*ARRAY_SIZE; i++)     {       myList.add(new MyType(i));     }         // initialize the array with unsorted data     for (int i = 0; i < ARRAY_SIZE; i++)     {       for (int j = 0; j < ARRAY_SIZE; j++)       {         myArray[i][j] = myList.get(j + i * ARRAY_SIZE);       }     }   }     // Call whenever I want to shuffle the list and reinsert into array   public void shuffle()   {     Collections.shuffle(myList);     for (int i = 0; i < ARRAY_SIZE; i++)     {       for (int j = 0; j < ARRAY_SIZE; j++)       {         myArray[i][j] = myList.get(j + i * ARRAY_SIZE);       }     }       }     public String toString()   {     StringBuilder sb = new StringBuilder();     for (int i = 0; i < myArray.length; i++)     {       for (int j = 0; j < myArray[i].length; j++)       {         sb.append(String.format("%4s", myArray[i][j]));       }       sb.append("\n");     }     return sb.toString();   }     public static void main(String[] args)   {     Fubar foo = new Fubar();     System.out.println(foo);     foo.shuffle();     System.out.println(foo);     foo.shuffle();     System.out.println(foo);   } }```
• 11-23-2008, 02:22 PM
Nuluvius
Ah yes, that is indeed quite easy to implement. Thank you for your help, I really apprechiate it. I should have absolutly no problems solving it now!!
• 11-23-2008, 05:36 PM
Nicholas Jordan
boolean shuffle(List l){}
See java.lang.UnsupportedOperationException
Code:

```try {   Collections.shuffle(myList); } catch(UnsupportedOperationException) {   return false; }```
which would be defensive coding.....
• 11-23-2008, 07:06 PM
Nuluvius
Nice, thank you also - very usefull.
• 11-23-2008, 07:15 PM
Nicholas Jordan
Okay, next thing is to use SecureRandom as the random source. In general, we may need to do shuffle as a preventive measure when using quicksort on degenerate cases - the array is already sorted usually - and going to SecureRandom early on prevents coding blunders later....the default implementation of shuffle provided by Collections is sufficient to prevent quicksort from hanging - that's what it's for.

newbies, oh the fun!