Example usage for java.util List listIterator

List of usage examples for java.util List listIterator

Introduction

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

Prototype

ListIterator<E> listIterator(int index);

Source Link

Document

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

Usage

From source file:MainClass.java

public static void main(String[] a) {
    List list = new LinkedList();
    list.add("A");
    list.add("B");
    list.add("C");
    list.add("D");

    ListIterator iter = list.listIterator(list.size());

    while (iter.hasPrevious()) {
        System.out.println(iter.previous());
    }/* w  ww.  jav a2  s. co m*/

}

From source file:MainClass.java

public static void main(String[] a) {

    List list = new LinkedList();
    list.add("A");
    list.add("B");
    list.add("C");
    list.add("D");

    ListIterator iter = list.listIterator(list.size());

    while (iter.hasPrevious()) {
        System.out.println(iter.previous());
        iter.add("a");
        break;//from  www  . jav  a2 s  . c o  m
    }
    System.out.println(list);
}

From source file:Main.java

public static void main(String[] a) {
    List<String> list = new LinkedList<String>();
    list.add("A");
    list.add("B");
    list.add("C");
    list.add("D");

    ListIterator iter = list.listIterator(1);
    while (iter.hasNext()) {
        System.out.println(iter.next());
    }//from  w  ww. j  a  v  a2s .  c  o  m

}

From source file:Main.java

public static void main(String[] a) {
    List<String> list = new LinkedList<String>();
    list.add("A");
    list.add("B");
    list.add("C");
    list.add("D");

    ListIterator<String> iter = list.listIterator(list.size());

    while (iter.hasPrevious()) {
        System.out.println(iter.previous());
        iter.add("a");
        break;/*from  ww w . j  ava2s  .  c om*/
    }
    System.out.println(list);
}

From source file:Main.java

public static void main(String... args) {
    final int MAX_VAL = 99999;
    List<Integer> linkedList = new LinkedList<Integer>();
    List<Integer> arrayList = new ArrayList<Integer>();
    for (int i = 0; i < MAX_VAL; i++) {
        linkedList.add(i);/*  w  w w .j a v  a2  s.c om*/
        arrayList.add(i);
    }
    long time = System.nanoTime();

    for (int i = 0; i < MAX_VAL; i++) {
        linkedList.add(MAX_VAL / 2, i);
    }
    System.out.println("LinkedList add:" + (System.nanoTime() - time));

    time = System.nanoTime();
    for (int i = 0; i < MAX_VAL; i++) {
        arrayList.add(MAX_VAL / 2, i);
    }
    System.out.println("ArrayList add:" + (System.nanoTime() - time));

    // Reset the lists
    linkedList = new LinkedList<Integer>();
    arrayList = new ArrayList<Integer>();
    for (int i = 0; i < MAX_VAL; i++) {
        linkedList.add(i);
        arrayList.add(i);
    }

    time = System.nanoTime();
    ListIterator<Integer> li = linkedList.listIterator(MAX_VAL / 2);
    for (int i = 0; i < MAX_VAL; i++) {
        li.add(i);
    }
    System.out.println("LinkedList iterator add:" + (System.nanoTime() - time));

    time = System.nanoTime();
    ListIterator<Integer> ali = arrayList.listIterator(MAX_VAL / 2);
    for (int i = 0; i < MAX_VAL; i++) {
        ali.add(i);
    }
    System.out.println("ArrayList iterator add:" + (System.nanoTime() - time));
}

From source file:Main.java

/**
 * Remove all null elements at the end of the list.
 *//*  w  w  w .j  a v  a 2  s  .co 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

private static Node getNearestAncestorNode(Node node, List<Node[]> list) {
    for (ListIterator<Node[]> i = list.listIterator(list.size()); i.hasPrevious();) {
        Node[] nodes = i.previous();
        if (isDescendant(node, nodes[0])) {
            return nodes[1];
        }//  ww  w. ja v a2 s  .  c o  m
    }
    return null;
}

From source file:Main.java

/**
 * Removes the <code>null</code> values from the list.
 * /* ww  w.j a v  a2s . c  om*/
 * @param values the values
 */
public static void removeNulls(List<?> values) {
    if (values != null) {
        for (ListIterator<?> i = values.listIterator(values.size()); i.hasPrevious();) {
            Object value = i.previous();
            if (value == null)
                i.remove();
        }
    }
}

From source file:Main.java

/**
 * Returns the last non-null element in the given list; or, if
 * there is no non-null element, it returns null.
 *
 * @param l The list. It may be null, in which case null is
 * returned.//from   w  w w .j av  a  2s .co  m
 *
 * @return The element.
 */

public static <E> E getLastNonNull(List<E> l) {
    if (l == null) {
        return null;
    }
    ListIterator<E> i = l.listIterator(l.size());
    while (i.hasPrevious()) {
        E e = i.previous();
        if (e != null) {
            return e;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns an iterator that iterates backwards over the given list.
 * The remove() method is not implemented and will throw UnsupportedOperationException.
 * The returned iterator does not support the remove() method.
 * /* w  w w  . j  a  v  a2  s.  co m*/
 * @throws NullPointerException if list is {@code null}
 */
public static <T> Iterator<T> reverseIterator(final List<T> list) {
    return new Iterator<T>() {
        ListIterator<T> iterator = list.listIterator(list.size());

        @Override
        public boolean hasNext() {
            return iterator.hasPrevious();
        }

        @Override
        public T next() {
            return iterator.previous();
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("remove() not supported"); //$NON-NLS-1$
        }
    };
}