Collections.shuffle(List): This method is available in java.util.Collections class of Java.
Syntax:
void java.util.Collections.shuffle(List<?> list)
This method takes one argument. This method randomly permutes the specified list using a default source of randomness.
Note: All permutations occur with approximately equal likelihood.
Parameters: One parameter is required for this method.
list: the list to be shuffled.
Throws:
1. UnsupportedOperationException - if the specified list or its list-iterator does not support the set operation.
Approach
Java
import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Collectionsshuffle {public static void main(String[] args) {List<Integer> arrlist = new ArrayList<Integer>();arrlist.add(12);arrlist.add(56);arrlist.add(899);arrlist.add(65);arrlist.add(5);Collections.shuffle(arrlist);System.out.println(arrlist);}}
Output:
[5, 56, 12, 65, 899]
No comments:
Post a Comment