Here you can find the source of indexOf(final Collection c, final Object elem)
Parameter | Description |
---|---|
c | The Collection. |
elem | the element to find the index of |
public static int indexOf(final Collection c, final Object elem)
//package com.java2s; /* $Id$/*w w w .j a v a 2s. co m*/ ******************************************************************************* * Copyright (c) 2009 Contributors - see below * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Bob Tarling ******************************************************************************* */ import java.util.Collection; import java.util.List; public class Main { /** * Get the index position of an element in a collection * * @param c The Collection. * @param elem the element to find the index of * @return the element index */ public static int indexOf(final Collection c, final Object elem) { if (c instanceof List) { return ((List) c).indexOf(elem); } else { int index = 0; for (Object element : c) { if (element == elem) { return index; } else { ++index; } } return -1; } } }