I have created simple shuffle method everyting seems okay but there is a problem which i can't find.
Here is my Code Below:
public static <E> void shuffle(List<? extends E> list) {
List<E> temp = new ArrayList<>();
while (list.size() != 0) {
E val = list.remove((int) (Math.random() * list.size()));
temp.add(val);
System.out.println(val);
}
System.out.println(temp);
list = temp;
}
This is the test case:
ArrayList<Integer> arr = new ArrayList<>();
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);
arr.add(5);
arr.add(6);
System.out.println(arr);
shuffle(arr);
System.out.println("-->" + arr);
The problem is last print out method shows that arr is empty --> [] It Should be something like that [4,3,1,2,6,5]
Problem could be this line but i did not understand why ?
--> list = temp;