adsense


Saturday, 31 October 2015

JAVA EXAMPLES COLLECTION SHUFFLE PAGE 161

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES COLLECTION SHUFFLE







Java Examples - Collection Shuffle


Problem Description:
How to shuffle the elements of a collection ?
Solution:
Following example how to shuffle the elements of a collection 
with the help of Collections.shuffle() method of Collections class.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
   public static void main(String[] argv) 
   throws Exception {
      String[] alpha = { "A", "E", "I", "O", "U" };
      List list = new ArrayList(alpha);
      Collections.shuffle(list);
      System.out.println("list");
   }   
}
Result:
The above code sample will produce the following result.
I
O
A
U
E


No comments: