在ArrayList中移动项目

所以我对Java还是比较新的,我一直在玩ArrayList的 – 我试图达到的是一个方法来做这样的事情:

Item 1 Item 2 Item 3 Item 4 

所以我试图将项目移到列表中,除非它已经在顶部,在这种情况下它将保持不变。 例如,如果项目3被移动,则列表将是:

 Item 1 Item 3 Item 2 Item 4 

从我现在的小小的理解,我想要的东西沿线:

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

我正在努力的部分是“向上移动”部分。 任何提示或代码示例如何实现这一点非常感谢。

我在寻找答案时遇到了这个老问题,我想我只是发布我find的解决scheme,以防别人经过这里寻找相同的东西。

为了交换两个元素,Collections.swap没问题。 但是如果我们想要移动更多的元素,还有一个更好的解决scheme,其中涉及到创造性地使用Collections.sublist和Collections.rotate,我没有想到,直到我看到它在这里描述:

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

这里有一个引用,但是去那里读一下你自己:

请注意,此方法可以有用地应用于子列表,以移动列表中的一个或多个元素,同时保留其余元素的顺序。 例如,下面的习语将索引j处的元素向前移动到位置k(它必须大于或等于j):

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

一个简单的交换对于ArrayList中的“向上移动”要好得多:

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

因为一个ArrayList使用一个数组,所以如果你从一个ArrayList中删除一个项目,它必须“移动”该项目之后的所有元素,以填充数组中的空隙。 如果您插入一个项目,它必须移动该项目后的所有元素,以腾出空间插入它。 如果arrays非常大,这些转换可能会非常昂贵。 既然你知道你想在列表中得到相同数量的元素,那么做一个这样的交换,就可以非常有效地将元素“移动”到列表中的另一个位置。

正如Chris Buckler和Michal Kreuzman所指出的那样,Collections类中还有一个方便的方法来将这三行代码减less为一行:

 Collections.swap(arrayList, i, i-1); 

你可以试试这个简单的代码,Collections.swap(list,i,j)就是你要找的。

  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); 

要向上移动,请删除然后添加。

删除 – ArrayList.remove并将返回的对象分配给一个variables
然后将此对象添加回所需的索引 – ArrayList.add(int index, E element)

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

应用recursion来重新排列一个数组列表中的项目

 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); } } } 

正如Mikkel在Collections.rotate之前发布的一个简单的方法。 我使用这种方法在列表中上下移动项目。

 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); } } 

移动元素相互之间是我需要在我的一个项目很多东西。 所以我写了一个小的util类,将列表中的元素移动到相对于另一个元素的位置。 随意使用(并改善;))

 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; } } 

要在列表中Move项目,只需添加:

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

要在列表中Swap两个项目,只需添加:

 // swap item 10 with 20 Collections.swap(ObjectList,10,20);