从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 祝福语驱散霉运的话简短