Example usage for java.util ListIterator set

List of usage examples for java.util ListIterator set

Introduction

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

Prototype

void set(E e);

Source Link

Document

Replaces the last element returned by #next or #previous with the specified element (optional operation).

Usage

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");

    ListIterator<String> listIterator = aList.listIterator();
    listIterator.next();//from   w ww  .  j a  va 2  s  .  c  o  m

    listIterator.set("100");
    for (String str : aList) {
        System.out.println(str);
    }
}

From source file:IteratorDemo.java

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

    al.add("C");//from   ww  w .jav  a  2 s .  c  o  m
    al.add("A");
    al.add("E");
    al.add("B");
    al.add("D");
    al.add("F");

    Iterator<String> itr = al.iterator();
    while (itr.hasNext()) {
        String element = itr.next();
        System.out.print(element + " ");
    }

    ListIterator<String> litr = al.listIterator();
    while (litr.hasNext()) {
        String element = litr.next();
        litr.set(element + "+");
    }

    itr = al.iterator();
    while (itr.hasNext()) {
        String element = itr.next();
        System.out.print(element + " ");
    }

    while (litr.hasPrevious()) {
        String element = litr.previous();
        System.out.print(element + " ");
    }
}

From source file:MainClass.java

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

    al.add("C");//from  ww  w  .  j  a  v a 2  s .  c o  m
    al.add("A");
    al.add("E");
    al.add("B");
    al.add("D");
    al.add("F");

    System.out.print("Original contents of al: ");
    Iterator<String> itr = al.iterator();
    while (itr.hasNext()) {
        String element = itr.next();
        System.out.print(element + " ");
    }
    System.out.println();

    ListIterator<String> litr = al.listIterator();
    while (litr.hasNext()) {
        String element = litr.next();
        litr.set(element + "+");
    }

    // Now, display the list backwards.
    System.out.print("Modified list backwards: ");
    while (litr.hasPrevious()) {
        String element = litr.previous();
        System.out.print(element + " ");
    }
}

From source file:Main.java

public static <E> void swap(final List<E> list, final int i, final int j) {
    if (list instanceof RandomAccess) {
        list.set(i, list.set(j, list.get(i)));
    } else {//from w  ww  .j  a  va  2s  .com
        final ListIterator<E> iterator = list.listIterator(i);
        iterator.set(list.set(j, iterator.next()));
    }
}

From source file:Main.java

public static <T> void symmetricSort(List<T> list) {
    @SuppressWarnings("unchecked")
    T[] array = (T[]) new Object[list.size()];
    for (int i = 0; i < list.size(); i++) {
        if (i % 2 == 0) {
            array[(list.size() - i) / 2 + (list.size() % 2 - 1)] = list.get(i);
        } else {/*from  ww  w .  j  a  va 2 s. c  om*/
            array[(list.size() + i) / 2] = list.get(i);
        }
    }

    int j = 0;
    ListIterator<T> it = list.listIterator();
    while (it.hasNext()) {
        it.next();
        it.set(array[j++]);
    }
}

From source file:Main.java

/**
 * Copies all of the elements from one list into another. After the
 * operation, the index of each copied element in the destination list will
 * be identical to its index in the source list. The destination list must
 * be at least as long as the source list. If it is longer, the remaining
 * elements in the destination list are unaffected.
 * <p>/*from  w w  w.j  av  a 2s  . com*/
 * This method runs in linear time.
 * @param <T> .
 * @param dest The destination list.
 * @param src The source list.
 * 
 * @return boolean isCopyValide
 */
public static <T> Boolean copy(List<? super T> dest, List<? extends T> src) {

    Boolean isCopyValide = null;

    int srcSize = src.size();
    if (srcSize > dest.size()) {
        isCopyValide = false;
        throw new IndexOutOfBoundsException("Source does not fit in dest");

    }

    if (srcSize < COPY_THRESHOLD || (src instanceof RandomAccess && dest instanceof RandomAccess)) {
        for (int i = 0; i < srcSize; i++) {
            dest.set(i, src.get(i));
        }
    } else {
        ListIterator<? super T> di = dest.listIterator();
        ListIterator<? extends T> si = src.listIterator();
        for (int i = 0; i < srcSize; i++) {
            di.next();
            di.set(si.next());
        }
    }

    isCopyValide = true;

    return isCopyValide;
}

From source file:edu.cornell.mannlib.vitro.webapp.auth.policy.ServletPolicyList.java

/**
 * Replace the first instance of this class of policy in the list. If no
 * instance is found, add the policy to the end of the list.
 */// w w  w  .ja v a2s  .com
public static void replacePolicy(ServletContext sc, PolicyIface policy) {
    if (policy == null) {
        return;
    }

    Class<?> clzz = policy.getClass();
    PolicyList policies = getPolicyList(sc);
    ListIterator<PolicyIface> it = policies.listIterator();
    while (it.hasNext()) {
        if (clzz.isAssignableFrom(it.next().getClass())) {
            it.set(policy);
            return;
        }
    }

    addPolicy(sc, policy);
}

From source file:mitm.common.util.StringReplaceUtils.java

/**
 * For each String in the list the non-XML characters are replaced. The returned list is the same instance as input
 * (it's an in place replacement)//from w w w  . ja  v  a2  s  .co  m
 */
public static List<String> replaceNonXML(List<String> input, String replaceWith) {
    Check.notNull(replaceWith, "replaceWith");

    if (input == null) {
        return null;
    }

    ListIterator<String> listIterator = input.listIterator();

    while (listIterator.hasNext()) {
        String line = listIterator.next();

        listIterator.set(StringReplaceUtils.replaceNonXML(line, "#"));
    }

    return input;
}

From source file:Main.java

public static <E> void swapAll(final List<E> list, final int off, final int len) {
    if (list instanceof RandomAccess) {
        final int last = off + len - 1;
        for (int i = off, j = last; i < j; i++, j--) {
            list.set(i, list.set(j, list.get(i)));
        }//from  w  ww  . ja v  a  2  s  . c om
    } else {
        final int end = off + len;
        final ListIterator<E> iteratorI = list.listIterator(off);
        final ListIterator<E> iteratorJ = list.listIterator(end);
        E tmp;

        while (iteratorI.nextIndex() < iteratorJ.nextIndex()) {
            tmp = iteratorI.next();
            iteratorI.set(iteratorJ.previous());
            iteratorJ.set(tmp);
        }
    }
}

From source file:Main.java

public static <E> void swapAll(final List<E> list) {
    if (list instanceof RandomAccess) {
        for (int i = 0, j = list.size() - 1; i < j; i++, j--) {
            list.set(i, list.set(j, list.get(i)));
        }/*from   w w  w .  j  a va  2  s .  c  o  m*/
    } else {
        final ListIterator<E> iteratorI = list.listIterator();
        final ListIterator<E> iteratorJ = list.listIterator(list.size());
        E tmp;

        while (iteratorI.nextIndex() < iteratorJ.nextIndex()) {
            tmp = iteratorI.next();
            iteratorI.set(iteratorJ.previous());
            iteratorJ.set(tmp);
        }
    }
}