Java List Move Item moveToFront(List aList, Object anObj)

Here you can find the source of moveToFront(List aList, Object anObj)

Description

Move the given object to the front of the list.

License

Open Source License

Declaration

public static void moveToFront(List aList, Object anObj) 

Method Source Code


//package com.java2s;

import java.util.*;

public class Main {
    /**/*  w w  w .  j a  v  a2  s.c  om*/
     * Moves the object at the given index to the front of the list.
     */
    public static void moveToFront(List aList, int anIndex) {
        if (anIndex > 0)
            moveToFront(aList, aList.get(anIndex));
    }

    /**
     * Move the given object to the front of the list.
     */
    public static void moveToFront(List aList, Object anObj) {
        if (anObj != null) {
            aList.remove(anObj);
            aList.add(0, anObj);
        }
    }

    /**
     * Returns the object at the given index (returns null object for null list or invalid index).
     */
    public static <T> T get(List<T> aList, int anIndex) {
        return aList == null || anIndex < 0 || anIndex >= aList.size() ? null : aList.get(anIndex);
    }

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

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

Related

  1. moveItems(List list, int[] indices, int moveby, boolean up)
  2. moveLeft(int offset, T element, List list)
  3. moveOnePositionUp(final List list, final T object)
  4. moveRight(int offset, T element, List list)
  5. moveRows(final List allRows, final List rowsToMove, final int index)
  6. moveTop(List list, int[] indices)
  7. moveUp(final List list, final T toMoveUp)
  8. moveUp(List list, List toMoveUp)
  9. moveUp(List list, int[] selectionIndexes)

  10. HOME | Copyright © www.java2s.com 2016