Java ArrayList get() 方法
示例
輸出列表中某個項的值
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars.get(0));
}
}
定義和用法
get()
方法返回列表中指定位置的項。
語法
public T get(int index)
T
指的是列表中項的資料型別。
引數值
引數 | 描述 |
---|---|
index | 必需。指定項在列表中的位置。 |
技術詳情
返回 | 指定位置的項。 |
---|---|
丟擲 | IndexOutOfBoundsException - 如果索引小於零,等於列表大小或大於列表大小。 |
相關頁面
❮ ArrayList 方法