Results 1 to 3 of 3
  1. #1
    frasifrasi is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default How do I shuffle an arraylist?

    I know you can use Collections.shuffle();

    But I was looking for an actual algorith that will shuffle an arraylist. I have been looking for a while and can't find anything. Does anyone know one?

    Alternatively, is there a library method to convert the arraylist into an array and then from array to arraylist again?

    Thanks.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default sedgewick

    Quote Originally Posted by frasifrasi View Post
    ...(snip )...was looking for an actual algorith that will shuffle an arraylist. I have been looking for a while and can't find anything. Does anyone know one?
    Algorithms in Java by Sedgewick,... it's pretty much either that or DEK. Most the other works stop short of deep analysis of algorithmic work. I would also try The Core Java series. After that, I would just write a custom routine.

    Quote Originally Posted by frasifrasi View Post
    Alternatively, is there a library method to convert the arraylist into an array and then from array to arraylist again?
    All Collections have a toArray method. Arrays (Java 2 Platform SE 5.0) does not generate a new array. One cannot List.add() on a list constructed from Arrays.asList()
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Shuffle a list externally

    Hello frasifrasi

    You can shuffle an array list using this simple method:
    Java Code:
    	public static ArrayList shuffle(ArrayList [COLOR="RoyalBlue"]list[/COLOR]){
    		if (list.size() > 0) {
    			ArrayList copy = new ArrayList();
    			for (Object object : list)
    				copy.add(object);
    			Random generator = new Random();
    			ArrayList result = new ArrayList();
    			do{
    				int index = (int) (generator.nextDouble() * (double) copy.size());
    				result.add(copy.remove(index));
    			} while (copy.size() > 0);
    			return result;
    		} else
    			return new ArrayList();
    	}
    It will return a shuffled copy of the list while leaving it unharmed. This may not be the best solution, but it was an easy one. ;)

    Additionally, you can extend the java.util.ArrayList class with this or some other method and add some generics to the mix. That will get rid of the following message:
    Java Code:
    Note: Main.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    Happy coding frasifrasi :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  2. How to Shuffle a particular String
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-06-2008, 08:38 PM
  3. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 09:48 PM
  4. New to arraylist
    By kleave in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 07:45 PM
  5. Help with code "perfect shuffle"
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:25 AM

Posting Permissions

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