From 57376e0f0f5f0a58a7e886eaf1b62f32b84db540 Mon Sep 17 00:00:00 2001 From: Jochen Hoenicke Date: Mon, 31 Jan 2000 09:56:48 +0000 Subject: fixed a bug in shuffle (it was duplicating some entries, removing others) --- java/util/Collections.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/java/util/Collections.java b/java/util/Collections.java index 4db8776cb..6ed996024 100644 --- a/java/util/Collections.java +++ b/java/util/Collections.java @@ -470,13 +470,17 @@ public class Collections { // Obtain a random position to swap with. nextIndex is used so that the // range of the random number includes the current position. - int swap = r.nextInt(i.nextIndex()); + int swap = r.nextInt(i.nextIndex()); // Swap the swapth element of the array with the next element of the // list. - Object o = i.previous(); - i.set(a[swap]); - a[swap] = o; + Object o = a[swap]; + a[swap] = a[i.previousIndex()]; + a[i.previousIndex()] = o; + + // Set the element in the original list accordingly. + i.previous(); + i.set(o); } } -- cgit v1.2.1