This is a simple way of how to shuffle an array or a list in Java, using Collections.shuffle(<List>);
There's 2 methods:
public static void shuffle(List list);
public static void shuffle(List list, Random rnd);
Usefull Link: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#shuffle(java.util.List)
You can use ArrayList(), or any other struct that extends <List>
You can found them here: http://docs.oracle.com/javase/6/docs/api/java/util/List.html
// Create the list
List list = new ArrayList();
// Shuffle the list - You will lose the older order
Collections.shuffle(list);
// ---------- SHUFFLE ARRAY ------------
// Create an array
String[] array = new String[20];
// Add elements
// Convert Array to List
List<String> aux = Arrays.asList(array);
// Shuffle the array
Collections.shuffle(aux);
// Convert the list to array again
array = aux.toArray();
Sem comentários:
Enviar um comentário