Collections.shuffle(List, Random): This method is available in java.util.Collections class of Java.
Syntax:
void java.util.Collections.shuffle(List<?> list, Random rnd)
This method takes two arguments. This method randomly permutes the specified list using the specified source of randomness.
Note: All permutations occur with equal likelihood assuming that the source of randomness is fair.
Parameters: Two parameters are required for this method.
list: the list to be shuffled.
rnd: the source of randomness to use to shuffle the list.
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;import java.util.Random;public class Collectionsshuffle2 {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);Random rnd = new Random();Collections.shuffle(arrlist, rnd);System.out.println(arrlist);}}
Output:
[65, 56, 12, 899, 5]
No comments:
Post a Comment