Moving items in an ArrayList

I played 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 

I am trying to move items up the list, unless it is on top, in which case it will remain the same. For example, if item 3 was moved, the list would look like this:

 Item 1 Item 3 Item 2 Item 4 

From my little understanding at the moment, I would like something like:

 IF arrayname index is not equal to 0 THEN move up ELSE do nothing 

The part I'm struggling with is the move up part. Any tips or code examples on how to achieve this are greatly appreciated.

+70
java arraylist
Feb 08 2018-11-21T00:
source share
8 answers

I ran into this old question in search of an answer, and I thought that I would just post the solution that I found if someone else goes here looking for the same thing.

To replace 2 elements, Collections.swap is fine. But if we want to move more elements, there is a better solution that involves the creative use of Collections.sublist and Collections.rotate, which I did not think about until I saw that it was described here:

http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#rotate%28java.util.List,%20int%29

Here is a quote, but go there and read everything for yourself:

Note that this method may be useful for subscriptions to move one or more items in a list, while maintaining the order of the remaining items. For example, the following idiom moves an element when index j moves to position k (which must be greater than or equal to j):

Collections.rotate(list.subList(j, k+1), -1);

+122
Jan 23 2018-12-23T00:
source share

A simple swap is much better for "moving something" in an ArrayList:

 if(i > 0) { Item toMove = arrayList.get(i); arrayList.set(i, arrayList.get(i-1)); arrayList.set(i-1, toMove); } 

Since an ArrayList uses an array, if you delete an element from an ArrayList, it must β€œshift” all the elements after that element up to fill the gap in the array. If you insert an element, it must move all the elements after that element to make room for it to be inserted. These shifts can become very expensive if your array is very large. Since you know that you want to get the same number of elements in the list, such a swap allows you to very effectively "move" an element to another place in the list.

As Chris Buckler and Michal Kreitzman emphasizes, there is even a convenient method in the Collections class that reduces these three lines of code to one:

 Collections.swap(arrayList, i, i-1); 
+63
Feb 08 2018-11-11T00:
source share

you can try this simple code, Collections.swap (list, i, j) is what you are 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); 
+29
Feb 08 2018-11-11T00:
source share

To move up, delete and then add.

Delete - ArrayList.remove and assign the returned object to a variable
Then add this object back to the required index - ArrayList.add(int index, E element)

http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html#add(int , E)

+22
Feb 08 '11 at 21:30
source share

As Mikkel posted before Collections.rotate an easy way. I use this method to move items up and down in a list.

 public static <T> void moveItem(int sourceIndex, int targetIndex, List<T> list) { if (sourceIndex <= targetIndex) { Collections.rotate(list.subList(sourceIndex, targetIndex + 1), -1); } else { Collections.rotate(list.subList(targetIndex, sourceIndex + 1), 1); } } 
+7
Aug 16 '16 at 12:26
source share

In the Move element in the list, simply add:

 // move item to index 0 Object object = ObjectList.get(index); ObjectList.remove(index); ObjectList.add(0,object); 

In Swap two items in the list simply add:

 // swap item 10 with 20 Collections.swap(ObjectList,10,20); 
+5
Oct 26 '17 at 23:18
source share

Using recursion to reorder items in an arraylist

 public class ArrayListUtils { public static <T> void reArrange(List<T> list,int from, int to){ if(from != to){ if(from > to) reArrange(list,from -1, to); else reArrange(list,from +1, to); Collections.swap(list, from, to); } } } 
+4
Dec 22 '15 at 16:05
source share

Moving an element in relation to each other is what I needed a lot in my project. Therefore, I wrote a small util class that moves an element in the list to a position relative to another element. Feel free to use (and improve;))

 import java.util.List; public class ListMoveUtil { enum Position { BEFORE, AFTER }; /** * Moves element `elementToMove` to be just before or just after `targetElement`. * * @param list * @param elementToMove * @param targetElement * @param pos */ public static <T> void moveElementTo( List<T> list, T elementToMove, T targetElement, Position pos ) { if ( elementToMove.equals( targetElement ) ) { return; } int srcIndex = list.indexOf( elementToMove ); int targetIndex = list.indexOf( targetElement ); if ( srcIndex < 0 ) { throw new IllegalArgumentException( "Element: " + elementToMove + " not in the list!" ); } if ( targetIndex < 0 ) { throw new IllegalArgumentException( "Element: " + targetElement + " not in the list!" ); } list.remove( elementToMove ); // if the element to move is after the targetelement in the list, just remove it // else the element to move is before the targetelement. When we removed it, the targetindex should be decreased by one if ( srcIndex < targetIndex ) { targetIndex -= 1; } switch ( pos ) { case AFTER: list.add( targetIndex + 1, elementToMove ); break; case BEFORE: list.add( targetIndex, elementToMove ); break; } } 
0
Jun 11 '15 at 12:19
source share



All Articles