Shuffle Elements In Java ArrayList
Chapter:
Data Structures
Last Updated:
21-07-2016 10:37:25 UTC
Program:
/* ............... START ............... */
import java.util.ArrayList;
import java.util.Collections;
public class JavaArryListShuffle {
public static void main(String a[]) {
ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add("five");
list.add("sixth");
list.add("seven");
Collections.shuffle(list);
System.out.println("Results after shuffle operation:");
for (String str : list) {
System.out.println(str);
}
Collections.shuffle(list);
System.out.println("Results after shuffle operation:");
for (String str : list) {
System.out.println(str);
}
}
}
/* ............... END ............... */
Output
Results after shuffle operation:
sixth
three
four
two
seven
one
five
Results after shuffle operation:
one
sixth
three
four
five
two
seven
Notes:
-
Shuffling an array or a list means that you are randomly re-arranging the content of that structure.
- The shuffle(List<?>) method is used to randomly permute the specified list using a default source of randomness.
Tags
Shuffle Elements, Java