Java.util.ArrayList.indexOf()方法实例
width="728" height="90" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_0" name="aswift_0" style="box-sizing: border-box; left: 0px; position: absolute; top: 0px;">
id="cproIframe_u1064372_1" width="728" height="90" src="http://pos.baidu.com/acom?adn=3&at=38&aurl=&cad=1&ccd=24&cec=UTF-8&cfv=17&ch=0&col=zh-CN&conOP=0&cpa=1&dai=1&dis=0<r=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DeWR-wMLUXkrt0q4QOfkhPn23dnQCPcU1Tam8XmgY12JeSRJfgS8Oi7EuLx4DqUInssZq-gvzCh5pxPopROllyq%26wd%3D%26eqid%3D9f9ef4e4000082a90000000555a4aff1<u=http%3A%2F%2Fwww.yiibai.com%2Fjava%2Futil%2Farraylist_indexof.html&lunum=6&n=90029059_cpr&pcs=1007x592&pis=10000x10000&ps=387x234&psr=1440x900&pss=1007x2297&qn=4410398d8bc0edf3&rad=&rsi0=728&rsi1=90&rsi5=4&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%230000FF&rss3=%23444444&rss4=%23008000&rss5=&rss6=%23e10900&rss7=&scale=&skin=&td_id=1064372&tn=text_default_728_90&tpr=1436856317179&ts=1&version=2.0&xuanting=0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u1064372&ti=Java.util.ArrayList.indexOf()%E6%96%B9%E6%B3%95%E5%AE%9E%E4%BE%8B%20-%20Java.util%E5%8C%85&tt=1436856317142.38.406.413" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="box-sizing: border-box;">
java.util.ArrayList.indexOf(Object) 方法返回指定元素的第一个匹配项的索引在此列表中,或者-1,如果此列表中不包含该元素。
声明
以下是java.util.ArrayList.indexOf()方法的声明
public int indexOf(Object o)
参数
-
o -- 要搜索的元素。
返回值
此方法返回指定元素的第一个匹配项的索引在此列表中,或者如果此列表中不包含该元素返回-1。
异常
-
NA
例子
下面的示例演示java.util.ArrayList.indexOf()方法的用法。
package com.yiibai; import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { // create an empty array list with an initial capacity ArrayList<String> arrlist = new ArrayList<String>(5); // use add() method to add values in the list arrlist.add("G"); arrlist.add("E"); arrlist.add("F"); arrlist.add("M"); System.out.println("Size of list: " + arrlist.size()); // let us print all the values available in list for (String value : arrlist) { System.out.println("Value = " + value); } // retrieving the index of element "E" 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
标签:indexOf()方法 indexOf