Example usage for java.util ListIterator remove

List of usage examples for java.util ListIterator remove

Introduction

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

Prototype

void remove();

Source Link

Document

Removes from the list the last element that was returned by #next or #previous (optional operation).

Usage

From source file:Main.java

public static void main(String args[]) {
    ArrayList<Integer> arrlist = new ArrayList<Integer>();

    arrlist.add(1);/*from ww w . j a  v a  2 s.co m*/
    arrlist.add(2);
    arrlist.add(3);
    arrlist.add(4);
    arrlist.add(5);

    ListIterator<Integer> iterator = arrlist.listIterator();
    while (iterator.hasNext()) {
        Integer i = iterator.next();
        iterator.remove();

    }
    System.out.println(arrlist);

}

From source file:Main.java

public static void main(String args[]) {
    ArrayList<Integer> arrlist = new ArrayList<Integer>();

    arrlist.add(1);/*  www.  j av  a 2  s  .  co m*/
    arrlist.add(2);
    arrlist.add(3);
    arrlist.add(4);
    arrlist.add(5);

    ListIterator<Integer> iterator = arrlist.listIterator(2);
    while (iterator.hasNext()) {
        Integer i = iterator.next();
        iterator.remove();

    }
    System.out.println(arrlist);

}

From source file:MovingPlanets.java

public static void main(String args[]) {
    String names[] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" };
    List planets = new ArrayList();
    for (int i = 0, n = names.length; i < n; i++) {
        planets.add(names[i]);//  www.  jav  a  2s .com
    }
    ListIterator lit = planets.listIterator();
    String s;
    lit.next();
    lit.next();
    s = (String) lit.next();
    lit.remove();
    lit.next();
    lit.next();
    lit.next();
    lit.add(s);
    lit.next(); // Gets back just added
    lit.previous();
    lit.previous();
    s = (String) lit.previous();
    lit.remove();
    lit.next();
    lit.next();
    lit.add(s);
    Iterator it = planets.iterator();
    while (it.hasNext()) {
        System.out.println(it.next());
    }
}

From source file:Main.java

public static void main(String[] args) {
    ArrayList<String> aList = new ArrayList<String>();

    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");

    // Get an object of ListIterator using listIterator() method

    ListIterator listIterator = aList.listIterator();
    listIterator.next();//from   w w w .  j a v  a  2  s .  c  o m
    listIterator.next();

    // remove element returned by last next method

    listIterator.remove();
    for (String str : aList) {
        System.out.println(str);
    }
}

From source file:Main.java

/**
 * Remove all null elements at the end of the list.
 *//*from   w  w w  . j a va  2 s.  c  o m*/
public static <T> void trimTail(List<T> l) {
    final ListIterator<T> it = l.listIterator(l.size());
    while (it.hasPrevious()) {
        if (it.previous() != null)
            return;
        it.remove();
    }
}

From source file:Main.java

/**
 * Removed null elements in-place./*from   www .j a  va 2s  .c  om*/
 * 
 * @param <T>
 * @param col
 */
public static <T> void clearNulls(List<T> col) {
    ListIterator<T> iter = col.listIterator();
    while (iter.hasNext()) {
        if (iter.next() == null) {
            iter.remove();
        }
    }
}

From source file:Main.java

/**
 *  Resizes the passed list to N entries. Will add the specified object if
 *  the list is smaller than the desired size, discard trailing entries if
 *  larger. This is primarily used to presize a list that will be accessed
 *  by index, but it may also be used to truncate a list for display.
 *
 *  @return The list, as a convenience for callers
 *
 *  @throws UnsupportedOperationException if the list does not implement
 *          <code>RandomAccess</code> and its list iterator does not
 *          support the <code>remove()</code> operation
 *//*from  w  ww.j  av a2  s. co m*/
public static <T> List<T> resize(List<T> list, int newSize, T obj) {
    if (list instanceof ArrayList)
        ((ArrayList<T>) list).ensureCapacity(newSize);

    if (list.size() < newSize) {
        for (int ii = list.size(); ii < newSize; ii++)
            list.add(obj);
    } else if (list.size() > newSize) {
        if (list instanceof RandomAccess) {
            for (int ii = list.size() - 1; ii >= newSize; ii--)
                list.remove(ii);
        } else {
            ListIterator<T> itx = list.listIterator(newSize);
            while (itx.hasNext()) {
                itx.next();
                itx.remove();
            }
        }
    }

    return list;
}

From source file:Main.java

/**
 * Keeps and returns the original list.//w  w  w  .  j a v  a 2 s.  c o  m
 * 
 * @param <T>
 * @param list
 * @return removes any duplicates in the list. Keeps the first occurrence of duplicates.
 */
public static <T> List<T> removeDuplicates(List<T> list) {
    HashSet<T> set = new HashSet<T>(list.size());
    ListIterator<T> i = list.listIterator();
    while (i.hasNext()) {
        T cur = i.next();
        if (set.contains(cur)) {
            i.remove();
        } else {
            set.add(cur);
        }
    }

    return list;
}

From source file:Main.java

public static long iterRemove(List list) {
    ListIterator iter = list.listIterator();
    long start, stop, result = 0;
    for (int i = 0; i < 100; i++) {
        iter.next();//from w w w.ja v a 2  s.  c o m
        start = System.nanoTime();
        iter.remove();
        stop = System.nanoTime();
        result += stop - start;
    }
    return result / 100;
}

From source file:edu.cornell.mannlib.vitro.webapp.utils.fields.FieldUtils.java

public static List<Individual> removeIndividualsAlreadyInRange(List<Individual> individuals,
        List<ObjectPropertyStatement> stmts, String predicateUri, String objectUriBeingEdited) {
    HashSet<String> range = new HashSet<String>();

    for (ObjectPropertyStatement ops : stmts) {
        if (ops.getPropertyURI().equals(predicateUri))
            range.add(ops.getObjectURI());
    }/*from   ww  w. j  a  v a2 s  .  c o  m*/

    int removeCount = 0;
    ListIterator<Individual> it = individuals.listIterator();
    while (it.hasNext()) {
        Individual ind = it.next();
        if (range.contains(ind.getURI()) && !(ind.getURI().equals(objectUriBeingEdited))) {
            it.remove();
            ++removeCount;
        }
    }

    return individuals;
}