Moving items around in an ArrayList
I came across this old question in my search for an answer, and I thought I would just post the solution I found in case someone else passes by here looking for the same.
For swapping 2 elements, Collections.swap is fine. But if we want to move more elements, there is a better solution that involves a creative use of Collections.sublist and Collections.rotate that I hadn't thought of until I saw it described here:
http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#rotate%28java.util.List,%20int%29
Here's a quote, but go there and read the whole thing for yourself too:
Note that this method can usefully be applied to sublists to move one or more elements within a list while preserving the order of the remaining elements. For example, the following idiom moves the element at index j forward to position k (which must be greater than or equal to j):
Collections.rotate(list.subList(j, k+1), -1);
you can try this simple code, Collections.swap(list, i, j) is what you looking for.
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
String toMoveUp = "3";
while (list.indexOf(toMoveUp) != 0) {
int i = list.indexOf(toMoveUp);
Collections.swap(list, i, i - 1);
}
System.out.println(list);
A simple swap is far better for "moving something up" in an ArrayList:
if(i > 0) {
Item toMove = arrayList.get(i);
arrayList.set(i, arrayList.get(i-1));
arrayList.set(i-1, toMove);
}
Because an ArrayList uses an array, if you remove an item from an ArrayList, it has to "shift" all the elements after that item upward to fill in the gap in the array. If you insert an item, it has to shift all the elements after that item to make room to insert it. These shifts can get very expensive if your array is very big. Since you know that you want to end up with the same number of elements in the list, doing a swap like this allows you to "move" an element to another location in the list very efficiently.
As Chris Buckler and Michal Kreuzman point out, there is even a handy method in the Collections class to reduce these three lines of code to one:
Collections.swap(arrayList, i, i-1);