What's the most optimum way of swapping two elements in a List?
Created Aug 24, 2001
John Zukowski The 1.4 version of Collections has a swap() method to do this for you. However, for earlier version of Java, you can swap two elements w/o an intermediate variable with:
list.set(index1, list.set(index2, list.get(index1)));
list.set(index1, list.set(index2, list.get(index1)));
This works because the set() method returns the original element.