Here you can find the source of moveElementInList(List
Parameter | Description |
---|---|
targetIndex | the new position for the object in the list. |
sourceIndex | the old position of the object in the list. |
public static Object moveElementInList(List<Object> list, int targetIndex, int sourceIndex)
//package com.java2s; //License from project: Creative Commons License import java.util.List; public class Main { /**/*from w w w. ja v a2s . c o m*/ * Moves the object at the source index of the list to the _target index of * the list and returns the moved object. * * @param targetIndex * the new position for the object in the list. * @param sourceIndex * the old position of the object in the list. * @return the moved object. * @exception IndexOutOfBoundsException * if either index isn't within the size range. */ public static Object moveElementInList(List<Object> list, int targetIndex, int sourceIndex) { if (targetIndex >= list.size() || targetIndex < 0) throw new IndexOutOfBoundsException("targetIndex=" + targetIndex + ", size=" + list.size()); //$NON-NLS-1$ //$NON-NLS-2$ if (sourceIndex >= list.size() || sourceIndex < 0) throw new IndexOutOfBoundsException("sourceIndex=" + sourceIndex + ", size=" + list.size()); //$NON-NLS-1$ //$NON-NLS-2$ Object object = list.get(sourceIndex); if (targetIndex != sourceIndex) { list.remove(sourceIndex); list.add(targetIndex, object); } return object; } }