方法indexOf(obj o)在Java中做什么?
java.util.ArrayList类的 indexOf(Object)方法返回此列表中指定元素的首次出现的索引;如果此列表不包含该元素,则返回-1。
示例
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<String> arrlist = new ArrayList<String>(5);
arrlist.add("G");
arrlist.add("E");
arrlist.add("F");
arrlist.add("M");
System.out.println("Size of list: " + arrlist.size());
for (String value : arrlist) {
System.out.println("Value = " + value);
}
int retval=arrlist.indexOf("E");
System.out.println("The element E is at index " + retval);
}
}输出
Size of list: 4 Value = G Value = E Value = F Value = M The element E is at index 1
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短