Example usage for java.util ListIterator previousIndex

List of usage examples for java.util ListIterator previousIndex

Introduction

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

Prototype

int previousIndex();

Source Link

Document

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

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("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();//  w  w  w.  ja va  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  ww w .  ja va2s  .  c  om
    System.out.println("Previous: " + listIterator.previousIndex());
    System.out.println("Next: " + listIterator.nextIndex());
}

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 .j a va 2  s.  c om
    // 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

/**
 * Finds an item by iterating through the specified iterator.
 * /*  ww  w . j a  v a 2 s .c o  m*/
 * @param item the object to find
 * @param iter the iterator to examine
 * @return list index of the item or -1 if the item was not found
 */
public static int findObject(Object item, ListIterator<?> iter) {
    while (iter.hasNext()) {
        Object o = iter.next();
        if (item == null ? o == null : item.equals(o)) {
            return iter.previousIndex();
        }
    }
    return -1;
}

From source file:Main.java

/**
 * Returns a list iterator that swaps all previous/next calls.
 * <p><b>Important:</b> The returned iterator violates the {@link ListIterator#nextIndex()} and {@link ListIterator#previousIndex()} specifications.
 *//* w  ww. java2  s .c  om*/
public static <E> ListIterator<E> reverse(ListIterator<E> iterator) {
    return new ListIterator<E>() {
        @Override
        public boolean hasNext() {
            return iterator.hasPrevious();
        }

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

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

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

        @Override
        public int nextIndex() {
            return iterator.previousIndex();
        }

        @Override
        public int previousIndex() {
            return iterator.nextIndex();
        }

        @Override
        public void remove() {
            iterator.remove();
        }

        @Override
        public void set(E e) {
            iterator.set(e);
        }

        @Override
        public void add(E e) {
            iterator.add(e);
        }
    };
}

From source file:Main.java

/**
 * Wraps an <code>ListIterator</code> and returns a <code>ListIterator</code>
 * that cannot modify the underlying list.
 * All methods that could be used to modify the list throw
 * <code>UnsupportedOperationException</code>
 * @param underlying original list iterator
 * @param <T> element type//www. ja  v  a 2 s. c o m
 * @return unmodifiable list iterator
 */
@Nonnull
public static <T> ListIterator<T> unmodifiableListIterator(final @Nonnull ListIterator<T> underlying) {
    return new ListIterator<T>() {
        public boolean hasNext() {
            return underlying.hasNext();
        }

        public T next() {
            return underlying.next();
        }

        public boolean hasPrevious() {
            return underlying.hasPrevious();
        }

        public T previous() {
            return underlying.previous();
        }

        public int nextIndex() {
            return underlying.nextIndex();
        }

        public int previousIndex() {
            return underlying.previousIndex();
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }

        public void set(T t) {
            throw new UnsupportedOperationException();
        }

        public void add(T t) {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:it.unimi.di.big.mg4j.document.tika.AbstractTikaDocumentFactory.java

@Override
public int fieldIndex(String fieldName) {
    // TODO: use a map
    ListIterator<TikaField> li = fields().listIterator();
    while (li.hasNext()) {
        if (li.next().mg4jName().equals(fieldName))
            return li.previousIndex();
    }/*  w  w w .  jav a  2  s. c o  m*/
    return -1;
}

From source file:ListOfLists.java

public int indexOf(Object o) {
    ListIterator e = listIterator();
    if (o == null) {
        while (e.hasNext()) {
            if (e.next() == null)
                return e.previousIndex();
        }/*w  ww.j a  va2  s .  c  om*/
    } else {
        Object el;
        while (e.hasNext()) {
            el = e.next();
            if (el.equals(o))
                return e.previousIndex();
        }
    }
    return -1;
}

From source file:org.jasig.i18n.translate.AutoTranslationService.java

public Map<String, String> getAutoUpdatedTranslationMap(Map<String, String> mainMap,
        Map<String, String> targetMap, String languageKey) {

    // assemble a set of keys represented in the main map but not in the
    // target language map
    Set<String> missing = mainMap.keySet();
    missing.removeAll(targetMap.keySet());

    // put the keys in a list so that we have a consistent ordering
    List<String> keys = new ArrayList<String>();
    keys.addAll(missing);//from   w ww .ja va 2  s  .  c o  m

    // assemble a list of untranslated messages in the same order as our
    // missing key list
    List<String> untranslatedMessages = new ArrayList<String>();
    for (String key : keys) {
        untranslatedMessages.add(mainMap.get(key));
    }

    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("key", this.apiKey);
    parameters.put("source", this.defaultLanguageKey);
    parameters.put("target", languageKey);
    parameters.put("query", untranslatedMessages);

    GoogleTranslationResponse response = this.restTemplate.getForObject(urlTemplate,
            GoogleTranslationResponse.class, parameters);
    List<Translation> translations = response.getTranslations();

    // iterate through the auto-translations, adding each to the target
    // map
    ListIterator<Translation> iter = translations.listIterator();
    while (iter.hasNext()) {
        Translation translation = iter.next();
        String key = keys.get(iter.previousIndex());
        targetMap.put(key, translation.getTranslatedText());
    }

    return targetMap;
}

From source file:com.google.gwt.emultest.java.util.ListTestBase.java

public void testListIteratorHasNextHasPreviousAndIndexes() {
    List l = makeEmptyList();//from ww w  .  j  a v  a2  s  . c  o m
    ListIterator i = l.listIterator();
    assertFalse(i.hasNext());
    assertFalse(i.hasPrevious());
    i.add(new Integer(1));
    assertEquals(1, i.nextIndex());
    assertEquals(0, i.previousIndex());
    i = l.listIterator();
    assertEquals(0, i.nextIndex());
    assertEquals(-1, i.previousIndex());
    assertTrue(i.hasNext());
    assertFalse(i.hasPrevious());
    i.next();
    assertEquals(1, i.nextIndex());
    assertEquals(0, i.previousIndex());
    assertFalse(i.hasNext());
    assertTrue(i.hasPrevious());
}