The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Random Algorithm Shuffling - The Easy Way!

Posted by Matt Gerrans on November 09, 2001 at 12:13 AM

Duh. As I was out on my bike ride today, it occurred to me that Java does have a method for sorting Lists in the API. I had a vague impression that I had seen a list somewhere that had it already, but I thought it was Python, because when I looked at the List interface in my Java docs, there was no shuffle(). Java's does have it, though, it is just a little trickier to find because it is a static method of Collections, rather than a method of List, or one of its implementations. So all the previous example code could be reduced to Collections.shuffle(itemList); (assuming you want to use the default source of randomness to randomly permute your list).

- mfg

  • biking Chin Long November 09, 2001 at 3:05 AM (4)
    • Weather Cycles Matt Gerrans November 09, 2001 at 3:40 AM (3)
      • hmmm Chin Loong November 10, 2001 at 5:42 AM (2)

Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us