publicclassShuffle{ publicstaticvoidmain(String[] args){ List<String> list = new ArrayList<>(Arrays.asList("either way is too late for regrets".split(" "))); System.out.println(list); Collections.shuffle(list); System.out.println(list);
shuffle(list); System.out.println(list); }
publicstaticvoidshuffle(List<String> list){ List<String> temp = new ArrayList<>(); Random r = new Random(); while(!list.isEmpty()) { int loc = r.nextInt(list.size()); temp.add(list.get(loc)); list.remove(loc); } list.addAll(temp); } }
The output of the above code is:
1 2 3
[either, way, is, too, late, for, regrets] [for, late, either, regrets, too, way, is] [too, either, for, regrets, is, way, late]