1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/* Shuffle elements of Java Vector example This java example shows how to shuffle elements of Java Vector object using shuffle method of Collections class. */ import java.util.Vector; import java.util.Collections; public class ShuffleElementsOfVectorExample { public static void main(String[] args) { //create a Vector object Vector v = new Vector(); //Add elements to Vector v.add("1"); v.add("2"); v.add("3"); v.add("4"); v.add("5"); System.out.println("Before shuffling, Vector contains : " + v); /* To shuffle elements of Java Vector use, static void shuffle(List list) method of Collections class. */ Collections.shuffle(v); System.out.println("After shuffling, Vector contains : " + v); } } /* Output would be Before shuffling, Vector contains : [1, 2, 3, 4, 5] After shuffling, Vector contains : [4, 3, 2, 1, 5] */ |
Add Comment