There are 2 ways to move an item to the desired position in an ArrayList.
1. Swap items
Collections.swap (myArray, i, 0);
ā Here, the āiā position will be moved to the 0th position, and all other elements in this range will remain as they are.
2. Move items
myArray.add (0, myArray.remove (i))
ā Here the item at position āiā will be deleted and added to the 0th position. Here, all other positions will be shifted when adding a new element to 0.
Hope this helps you understand the difference between swap and position offset. Use the solution according to your requirements.
source share