手册
目录
收藏846
分享
阅读982
更新时间2025-07-22
前言:
定义和用法addAll() 方法将集合中的所有元素添加到列表中。如果提供了索引,则新元素将被放置在指定索引处,并将列表中该索引之后的所有元素向前移动。如果未提供
addAll() 方法将集合中的所有元素添加到列表中。
如果提供了索引,则新元素将被放置在指定索引处,并将列表中该索引之后的所有元素向前移动。
如果未提供索引,则新元素将被放置在列表的末尾。
教程:Java ArrayList
将一个列表中的元素添加到另一个列表中:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList cars = new ArrayList();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
ArrayList brands = new ArrayList();
brands.add("Microsoft");
brands.add("W3School");
brands.add("Apple");
brands.addAll(cars);
System.out.println(brands);
}
}
点击 "运行实例" 按钮查看在线实例
在列表的指定位置添加元素:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList cars = new ArrayList();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
ArrayList brands = new ArrayList();
brands.add("Microsoft");
brands.add("W3School");
brands.add("Apple");
brands.addAll(1, cars);
System.out.println(brands);
}
}
点击 "运行实例" 按钮查看在线实例
以下之一:
public boolean addAll(Collectionitems) public boolean addAll(int index, Collection items)
点击 "运行实例" 按钮查看在线实例
其中,T 表示列表中元素的数据类型。
| 参数 | 描述 |
|---|---|
| index | 可选。要在列表中添加元素的位置。 |
| items | 必需。一个包含要添加到列表中的元素的集合。 |
| 返回: | 如果列表发生变化,则返回 true;否则返回 false。 |
|---|---|
| 抛出: |
|
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
9
精选课程
共5课时
17.2万人学习
共49课时
77万人学习
共29课时
61.7万人学习
共25课时
39.3万人学习
共43课时
71万人学习
共25课时
61.6万人学习
共22课时
23万人学习
共28课时
33.9万人学习
共89课时
125万人学习