tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Java > Random Numbers > Simple Shuffle

Simple Shuffle 

The following code shows shuffling a list using Collections API.

File Name  :  
com/bethecoder/tutorials/random/SimpleShuffle.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.random;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SimpleShuffle {

  public static void main(String[] args) {
    
    List list = new ArrayList();
    list.addAll(Arrays.asList(new String [] {
        "ONE""TWO""THREE""FOUR""FIVE""SIX"
    }));
    
    shuffle(list);
    
    
    List list2 = new ArrayList();
    list2.addAll(Arrays.asList(new Integer [] {
        243454567909324567823 
    }));
    
    shuffle(list2);
    
  }
  
  public static void shuffle(List list) {
    
    System.out.println("Before : " + list);
    Collections.shuffle(list);
    System.out.println("after : " + list);
  }
}
   

It gives the following output,
Before : [ONE, TWO, THREE, FOUR, FIVE, SIX]
after : [SIX, TWO, THREE, ONE, FOUR, FIVE]

Before : [24, 345, 45, 67, 90, 93, 245, 678, 23]
after : [93, 67, 345, 678, 245, 90, 24, 23, 45]


                    Print

   



Books Recommendation

 
 
bl  br