Example usage for java.util ListIterator nextIndex

List of usage examples for java.util ListIterator nextIndex

Introduction

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

Prototype

int nextIndex();

Source Link

Document

Returns the index of the element that would be returned by a subsequent call to #next .

Usage

From source file:Main.java

public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("Oracle");
    list.add("SQL");
    list.add("CSS");
    list.add("XML");
    System.out.println("List: " + list);
    // Get the list iterator
    ListIterator<String> iterator = list.listIterator();
    while (iterator.hasNext()) {
        int index = iterator.nextIndex();
        String element = iterator.next();
        System.out.println("Index=" + index + ", Element=" + element);
    }/*from  w  w  w . ja va 2  s  .com*/
    // Reuse the iterator to iterate from the end to the beginning
    while (iterator.hasPrevious()) {
        int index = iterator.previousIndex();
        String element = iterator.previous();
        System.out.println("Index=" + index + ",  Element=" + element);
    }
}

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("java2s.com");

    ListIterator<String> listIterator = aList.listIterator();
    System.out.println("Previous: " + listIterator.previousIndex());
    System.out.println("Next: " + listIterator.nextIndex());

    // advance current position by one using next method
    listIterator.next();//from   w  w w.  j a v a  2  s.  co m
    System.out.println("Previous: " + listIterator.previousIndex());
    System.out.println("Next: " + listIterator.nextIndex());
}

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();
    System.out.println("Previous: " + listIterator.previousIndex());
    System.out.println("Next: " + listIterator.nextIndex());

    // advance current position by one using next method
    listIterator.next();/*from w  ww. jav  a 2s  .c o  m*/
    System.out.println("Previous: " + listIterator.previousIndex());
    System.out.println("Next: " + listIterator.nextIndex());
}

From source file:Main.java

public static <E> int indexOf(List<E> c, E item, BiPredicate<? super E, ? super E> equalTester) {
    ListIterator<E> iter = c.listIterator();
    while (iter.hasNext()) {
        int index = iter.nextIndex();
        if (equalTester.test(iter.next(), item)) {
            return index;
        }//from w w w . j  a v a2  s . c  o  m
    }
    return -1;
}

From source file:Main.java

/**
 * Returns the indexth element from the list by using the list iterator to navigate.
 * @param <T> the element type/*from   w  w w.  j a v  a 2  s  .co m*/
 * @param it the list iterator
 * @param index the target index
 * @return the value
 */
private static <T> T get(ListIterator<? extends T> it, int index) {
    T obj = null;
    int pos = it.nextIndex();
    if (pos <= index) {
        do {
            obj = it.next();
        } while (pos++ < index);
    } else {
        do {
            obj = it.previous();
        } while (--pos > index);
    }
    return obj;
}

From source file:Main.java

public static <E> E setInSortedList(List<E> list, int index, E item, Comparator<? super E> comparator,
        BiPredicate<? super E, ? super E> distincter) {
    if (distincter != null) {
        ListIterator<E> iter = list.listIterator();
        while (iter.hasNext()) {
            int currIndex = iter.nextIndex();
            E currItem = iter.next();//from  w w  w  .  j a v  a  2  s  . co m
            if (index != currIndex && distincter.test(currItem, item)) {
                return null;
            }
        }
    }
    E previousItem = list.set(index, item);
    list.sort(comparator);
    return previousItem;
}

From source file:Main.java

public static <E> boolean addToSortedList(List<E> list, E item, Comparator<? super E> comparator,
        BiPredicate<? super E, ? super E> distincter) {
    int addIndex = list.size();
    boolean foundAddIndex = false;
    ListIterator<E> iter = list.listIterator();
    while (iter.hasNext()) {
        int currIndex = iter.nextIndex();
        E currItem = iter.next();/*www . java 2s.  co m*/
        if (distincter != null && distincter.test(currItem, item)) {
            return false;
        }
        if (!foundAddIndex) {
            int comparison = comparator.compare(currItem, item);
            if (comparison > 0) {
                addIndex = currIndex;
                // if we don't have to check the remaining items for distinct we can break the loop now
                if (distincter == null) {
                    break;
                }
                foundAddIndex = true;
            } else if (comparison == 0) {
                addIndex = currIndex + 1;
                // if we don't have to check the remaining items for distinct we can break the loop now
                if (distincter == null) {
                    break;
                }
                foundAddIndex = true;
            }
        }
    }
    list.add(addIndex, item);
    return true;
}

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 ww  w  . j  av  a  2 s . c o m
    } 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)));
        }/*w ww .jav a  2s. 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);
        }
    }
}

From source file:samples.com.axiomine.largecollections.util.KryoListSample.java

public static void workOnKryoList(KryoList<Integer> lst) {
    try {//ww  w.j  av a2s.c  o m
        ListIterator<Integer> it = lst.listIterator();

        for (int i = 0; i < 10; i++) {
            boolean b = lst.add(i);
            Assert.assertEquals(true, true);
        }
        System.out.println("Size of map=" + lst.size());
        System.out.println("Value for key 0=" + lst.get(0));
        ;
        System.out.println("Now remove key 0");
        try {
            int i = lst.remove(0);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        int i = lst.remove(9);
        System.out.println("Value for deleted key=" + i);
        ;
        System.out.println("Size of map=" + lst.size());

        lst.close();
        boolean b = false;
        try {
            lst.add(9);
        } catch (Exception ex) {
            System.out.println("Exception because acces after close");
            b = true;
        }
        lst.open();
        System.out.println("Open again");
        b = lst.add(9);

        Assert.assertEquals(true, b);
        i = lst.set(9, 99);
        Assert.assertEquals(99, i);
        i = lst.set(5, 55);
        Assert.assertEquals(55, i);
        i = lst.set(0, 100);
        Assert.assertEquals(100, i);
        System.out.println(lst.get(0));
        System.out.println(lst.get(5));
        System.out.println(lst.get(9));
        System.out.println("Now put worked. Size of map should be 10. Size of the map =" + lst.size());

        Iterator<Integer> iter = lst.iterator();
        try {
            while (iter.hasNext()) {
                i = iter.next();
                System.out.println("From ITerator = " + i);
            }
        } finally {
            //Always close and iterator after use. Otherwise you will not be able to call the clear function
            ((Closeable) iter).close();
        }

        ListIterator<Integer> lstIter = lst.listIterator();
        try {
            while (lstIter.hasNext()) {
                i = lstIter.next();
                System.out.println("From List Iterator = " + i);
                System.out.println("From List Iterator Next Index= " + lstIter.nextIndex());
            }
        } finally {
            //Always close and iterator after use. Otherwise you will not be able to call the clear function
            ((Closeable) lstIter).close();
        }

        lstIter = lst.listIterator(5);
        try {
            while (lstIter.hasNext()) {
                i = lstIter.next();
                System.out.println("From List Iterator = " + i);
                System.out.println("From List Iterator Next Index= " + lstIter.nextIndex());
            }
        } finally {
            //Always close and iterator after use. Otherwise you will not be able to call the clear function
            ((Closeable) lstIter).close();
        }

        System.out.println("---");
        lstIter = lst.listIterator(9);
        try {
            while (lstIter.hasPrevious()) {
                i = lstIter.previous();
                System.out.println("From List Iterator Previous= " + i);
                System.out.println("From List Iterator Previous Index= " + lstIter.previousIndex());
            }
        } finally {
            //Always close and iterator after use. Otherwise you will not be able to call the clear function
            ((Closeable) lstIter).close();
        }

        System.out.println("----------------------------------");
        lstIter = lst.listIterator();
        try {
            while (lstIter.hasNext()) {
                i = lstIter.next();
                System.out.println("Iterating Forward = " + i);
            }

        } finally {
            //Always close and iterator after use. Otherwise you will not be able to call the clear function
            ((Closeable) lstIter).close();
        }

        System.out.println("Now Serialize the List");
        File serFile = new File(System.getProperty("java.io.tmpdir") + "/x.ser");
        FileSerDeUtils.serializeToFile(lst, serFile);
        System.out.println("Now De-Serialize the List");
        lst = (KryoList<Integer>) FileSerDeUtils.deserializeFromFile(serFile);
        System.out.println("After De-Serialization " + lst);
        System.out.println("After De-Serialization Size of map should be 10. Size of the map =" + lst.size());
        printListCharacteristics(lst);
        System.out.println("Now calling lst.clear()");
        lst.clear();
        System.out.println("After clear list size should be 0 and =" + lst.size());
        lst.add(0);
        System.out.println("Just added a record and lst size =" + lst.size());
        System.out.println("Finally Destroying");
        lst.destroy();

        System.out.println("Cleanup serialized file");
        FileUtils.deleteQuietly(serFile);

    } catch (Exception ex) {
        throw Throwables.propagate(ex);
    }

}