Java ListIterator Usage indexOf(List list, E object)

Here you can find the source of indexOf(List list, E object)

Description

Returns the index of object in list or -1 if the list does not contain the object.

License

Open Source License

Parameter

Parameter Description
E the element type of the list
list the list
object the object to search for

Return

the index of the object in the list or -1

Declaration

public static <E> int indexOf(List<E> list, E object) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.List;
import java.util.ListIterator;

public class Main {
    /**//  w  w  w.j  ava  2  s . c o  m
     * Returns the index of <code>object</code> in <code>list</code> or
     * <code>-1</code> if the list does not contain the object.
     * 
     * @param <E>
     *          the element type of the list
     * @param list
     *          the list
     * @param object
     *          the object to search for
     * 
     * @return the index of the object in the list or -1
     */
    public static <E> int indexOf(List<E> list, E object) {

        for (ListIterator<E> it = list.listIterator(); it.hasNext();) {
            if (it.next().equals(object)) {
                return it.previousIndex();
            }
        }

        return -1;
    }
}

Related

  1. getCurrentElement(ListIterator listIterator)
  2. getLastNonNull(List l)
  3. goToFirst(ListIterator iterator)
  4. gotoIndex(ListIterator iterator, int index)
  5. incrementalComponentWiseAverageArbitraryDepth(Object average, int n, Object newItems)
  6. insertBefore(List list, E... elements)
  7. intern(ListIterator in)
  8. internStringsInList(List list)
  9. isEqual(java.util.List list1, java.util.List list2)