So I'm still fairly new to Java and I've been playing around with ArrayList's - what I'm trying to achieve is a method to do something like this:
Item 1
Item 2
Item 3
Item 4
Item 1
Item 3
Item 2
Item 4
IF arrayname index is not equal to 0
THEN move up
ELSE do nothing
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:
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);