Java ArrayList.get() 方法及代码示例
获取列表指定索引位置的元素
定义
public E get(int index)
参数
参数类型 | 参数名称 | 参数描述 |
---|---|---|
int | index | 指定位置的索引 |
类型参数 | E | 列表元素的类型 |
返回值
返回列表中指定索引位置的元素
抛出的异常
IndexOutOfBoundsException
index 超出索引范围(index 小于 0 或 index 大于等于 size())
说明
本方法由 List<E>
接口的 get()
方法指派
重写了继承自 AbstractList<E>
类的 get()
方法
示例
使用 get() 获取数组列表元素的示例
package com.yi21.arraylist; import java.util.ArrayList; public class Yi21ArraysListGet { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("Hello"); list.add("World"); list.add("21yi"); System.out.println(list.get(0) + ", " + list.get(2)); } }
执行结果为 :
Hello, 21yi