# Thread: 2D Array Random Shuffle

1. Member
Join Date
Feb 2008
Posts
12
Rep Power
0

## 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.

2. Member
Join Date
Feb 2008
Posts
12
Rep Power
0
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.
Last edited by Nuluvius; 11-23-2008 at 02:07 AM.

3. why not change it to a 1-D array or list, shuffle it, then lay it out in 2-D after the shuffle?

4. Member
Join Date
Feb 2008
Posts
12
Rep Power
0
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?

5. 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":
Java 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:
Java 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++)
{
}

// 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);
}
}```

6. Member
Join Date
Feb 2008
Posts
12
Rep Power
0
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!!

7. ## boolean shuffle(List l){}

See java.lang.UnsupportedOperationException
Java Code:
```try
{
Collections.shuffle(myList);
}
catch(UnsupportedOperationException)
{
return false;
}```
which would be defensive coding.....

8. Member
Join Date
Feb 2008
Posts
12
Rep Power
0
Nice, thank you also - very usefull.

9. 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!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•