从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 生日广州男生祝福语简短