Here you can find the source of indexOf(Collection
Parameter | Description |
---|---|
stringCollection | collection of strings |
str | string to search for |
static public int indexOf(Collection<String> stringCollection, String str)
//package com.java2s; /******************************************************************************* * Copyright (c) 2015 ARM Ltd. and others * 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:/* w w w . j a va2 s . co m*/ * ARM Ltd and ARM Germany GmbH - Initial API and implementation *******************************************************************************/ import java.util.Collection; public class Main { /** * Returns index of string in a string collection * @param stringCollection collection of strings * @param str string to search for * @return index of the string if found, otherwise -1 */ static public int indexOf(Collection<String> stringCollection, String str) { if (str != null && stringCollection != null) { int i = 0; for (String s : stringCollection) { if (s.equals(str)) { return i; } i++; } } return -1; } /** * Returns index of string in a string array * @param stringArray collection of strings * @param str string to search for * @return index of the string if found, otherwise -1 */ static public int indexOf(String[] stringArray, String str) { if (str != null && stringArray != null) { int i = 0; for (String s : stringArray) { if (s.equals(str)) { return i; } i++; } } return -1; } }