mirror of
https://github.com/corda/corda.git
synced 2025-01-22 12:28:11 +00:00
implement Collections.shuffle
This commit is contained in:
parent
8659c709b7
commit
1d04fed6de
@ -13,6 +13,25 @@ package java.util;
|
||||
public class Collections {
|
||||
private Collections() { }
|
||||
|
||||
public static void shuffle(List list, Random random) {
|
||||
Object[] array = toArray(list, new Object[list.size()]);
|
||||
for (int i = 0; i < array.length; ++i) {
|
||||
int j = random.nextInt(array.length);
|
||||
Object tmp = array[i];
|
||||
array[i] = array[j];
|
||||
array[j] = tmp;
|
||||
}
|
||||
|
||||
list.clear();
|
||||
for (int i = 0; i < array.length; ++i) {
|
||||
list.add(array[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public static void shuffle(List list) {
|
||||
shuffle(list, new Random());
|
||||
}
|
||||
|
||||
static <T> T[] toArray(Collection collection, T[] array) {
|
||||
Class c = array.getClass().getComponentType();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user