Java List Move Item move(List aList, int anIndex1, int anIndex2)

Here you can find the source of move(List aList, int anIndex1, int anIndex2)

Description

Moves the object at index 1 to index 2.

License

Open Source License

Declaration

public static void move(List aList, int anIndex1, int anIndex2) 

Method Source Code


//package com.java2s;

import java.util.*;

public class Main {
    /**//from w ww  .j  a v a2 s  .  c  om
     * Moves the object at index 1 to index 2.
     */
    public static void move(List aList, int anIndex1, int anIndex2) {
        // If either index is invalid, return
        if (anIndex1 < 0 || anIndex1 >= aList.size() || anIndex2 < 0 || anIndex2 >= aList.size())
            return;

        // Remove object and re-insert and desired index
        Object obj = aList.remove(anIndex1);
        aList.add(anIndex2, obj);
    }

    /**
     * Returns the size of a list (accepts null list).
     */
    public static int size(List aList) {
        return aList == null ? 0 : aList.size();
    }

    /**
     * Removes given object from given list (accepts null list).
     */
    public static boolean remove(List aList, Object anObj) {
        return aList == null ? false : aList.remove(anObj);
    }

    /**
     * Removes range of objects from given list (from start to end, not including end).
     */
    public static void remove(List aList, int start, int end) {
        for (int i = end - 1; i >= start; i--)
            aList.remove(i);
    }

    /**
     * Adds an object to the given list and returns list (creates list if missing).
     */
    public static <T> List<T> add(List<T> aList, T anObj) {
        // If list is null, create list
        if (aList == null)
            aList = new Vector();

        // Add object
        aList.add(anObj);

        // Return list
        return aList;
    }
}

Related

  1. addRemoveChangeToString(int from, int to, List list, List removed)
  2. canMoveUp(List list, int[] indices)
  3. getLongestLine(String text, List partsToRemove, String separator)
  4. getRemoveAll(List list1, Collection list2)
  5. minus(List initialList, List elementsToRemove)
  6. move(List collection, int indexToMoveFrom, int indexToMoveAt)
  7. moveBackward(final List list, final int[] indices)
  8. moveBefore(List list, T element, T referenceElement)
  9. moved(List old, List nu, Collection added, Collection removed)