从Java中的ArrayList检索元素
可以使用java.util.ArrayList.get()方法从Java中的ArrayList中检索元素。该方法具有单个参数,即返回的元素的索引。
演示此的程序如下
示例
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String args[]) throws Exception {
List aList = new ArrayList();
aList.add("James");
aList.add("George");
aList.add("Bruce");
aList.add("Susan");
aList.add("Martha");
System.out.println("The element at index 3 in the ArrayList is: " + aList.get(3));
}
}上面程序的输出如下
The element at index 3 in the ArrayList is: Susan
现在让我们了解上面的程序。创建ArrayListaList。然后,使用ArrayList.add()将元素添加到ArrayList中。
然后,使用ArrayList.get()方法检索索引3处的元素,并显示该元素。演示这的代码片段如下
List aList = new ArrayList();
aList.add("James");
aList.add("George");
aList.add("Bruce");
aList.add("Susan");
aList.add("Martha");
System.out.println("The element at index 3 in the ArrayList is: " + aList.get(3));热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短