Java ArrayList set() 方法
示例
替换列表中的一个项
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");
cars.set(0, "Opel");
System.out.println(cars);
}
}
定义和用法
set() 方法替换列表中指定位置的项,并返回该位置之前存在的项。
语法
public T set(int index, T item)
T 指的是列表中项的数据类型。
参数值
| 参数 | 描述 |
|---|---|
| index | 必需。要替换的项的位置。 |
| item | 必需。要放置在指定位置的新项。 |
技术详情
| 返回 | 列表中指定位置之前存在的项。 |
|---|---|
| 抛出 | IndexOutOfBoundsException - 如果索引小于零,等于列表大小或大于列表大小。 |
相关页面
❮ ArrayList 方法