Example usage for java.util List remove

List of usage examples for java.util List remove

Introduction

In this page you can find the example usage for java.util List remove.

Prototype

E remove(int index);

Source Link

Document

Removes the element at the specified position in this list (optional operation).

Usage

From source file:Main.java

public static List filter(String[] strs, List list) {
    for (int i = 0; i < strs.length; i++) {
        while (list.contains(strs[i]))
            list.remove(strs[i]);
    }//ww  w .j a v a2s . c o  m
    return list;
}

From source file:Main.java

public static void resize(List<?> l, int n) {
    if (l.size() > n) {
        while (l.size() > n) {
            l.remove(l.size() - 1);
        }// www  .  ja va  2  s .co  m
    } else {
        while (l.size() < n) {
            l.add(null);
        }
    }
}

From source file:Main.java

public static <T, V extends T> List<T> removeObjectList(List<T> list, V o) {
    if (list == null) {
        list = new ArrayList<T>(1);
    }/*  w ww.  j ava  2s  .  co  m*/
    list.remove(o);
    return list;
}

From source file:Main.java

private static <T> T remove(List<T> list, int position) {
    if (list.isEmpty()) {
        return null;
    }/*from w  ww . j a  v  a2s .  co m*/
    return list.remove(position);
}

From source file:Main.java

public static <T> List<T> removeObjectList(List<T> list, int i) {
    if (list == null) {
        list = new ArrayList<T>(1);
    }/* ww  w . j a  va  2s  . co  m*/
    list.remove(i);
    return list;
}

From source file:Main.java

/**
 * Removes the first occurrence in the list of the specified object, using
 * object identity (==) not equality as the criterion for object presence. If
 * this list does not contain the element, it is unchanged.
 *
 * @param l The {@link List} from which to remove the object
 * @param o The object to be removed./*from w ww  . j a  v a 2 s  .c  o m*/
 * @return Whether or not the List was changed.
 */
public static <T> boolean removeObject(List<T> l, T o) {
    int i = 0;
    for (Object o1 : l) {
        if (o == o1) {
            l.remove(i);
            return true;
        } else
            i++;
    }
    return false;
}

From source file:Main.java

public static <T> void resize(List<T> l, int n, T filler) {
    if (l.size() > n) {
        while (l.size() > n) {
            l.remove(l.size() - 1);
        }/* w  w  w .  ja va2 s .  co m*/
    } else {
        while (l.size() < n) {
            l.add(filler);
        }
    }
}

From source file:Main.java

/**
 * This method does the same as {@link #moveLeft(int, Object, List)} but it moves the specified element
 * to the right instead of the left./* w  ww  .  j  a v a 2 s.c om*/
 */
public static <T> List<T> moveRight(int offset, T element, List<T> list) {
    final int elementIndex = list.indexOf(element);

    if (elementIndex == -1) {
        throw new NoSuchElementException("Element not found in provided list.");
    }

    if (offset == 0) {
        return list;
    } else {
        int newElementIndex = elementIndex + offset;

        // Ensure that the element will not move off the end of the list.
        if (newElementIndex >= list.size()) {
            newElementIndex = list.size() - 1;
        } else if (newElementIndex < 0) {
            newElementIndex = 0;
        }

        List<T> result = new ArrayList<>(list);
        result.remove(element);

        result.add(newElementIndex, element);

        return result;
    }
}

From source file:Main.java

public static <T> List<T> subtract(final Collection<T> a, final Collection<T> b) {
    List<T> list = new ArrayList<T>(a);
    for (T element : b) {
        if (list.contains(element)) {
            list.remove(element);
        }/*from www . jav a2  s  . c  om*/
    }
    return list;
}

From source file:Main.java

public static <U> List<U> getDifference(Iterable<U> X, Iterable<U> A) {
    List<U> list = new ArrayList<U>();

    for (U x : X) {
        list.add(x);//w w w .jav  a  2 s.  c o  m
    }

    for (U a : A) {
        list.remove(a);
    }

    return list;
}