Here you can find the source of indexOf(List
object
in list
or -1
if the list does not contain the object.
Parameter | Description |
---|---|
E | the element type of the list |
list | the list |
object | the object to search for |
public static <E> int indexOf(List<E> list, E object)
//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; } }