ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# Java `ArrayList addAll(int index, Collection c)`方法 > 原文: [https://beginnersbook.com/2013/12/java-arraylist-addall-int-index-collection-c-method-example/](https://beginnersbook.com/2013/12/java-arraylist-addall-int-index-collection-c-method-example/) 在上一个教程中,我们共享了[`addAll(Collection c)`方法](https://beginnersbook.com/2013/12/java-arraylist-addallcollection-c-method-example/)的示例,该方法用于在列表末尾添加`Collection c`的所有元素。在这里,我们将看到另一个变体`add(int index, Collection c)`,它在列表的指定索引处添加`c`的所有元素。 `public boolean addAll(int index, Collection c)` ### 例 在这个例子中,我们有两个`String`类型的`ArrayList`,我们在第一个`arraylist`的第 3 个位置(`index = 2`)添加第二个`arraylist`的元素。 ```java package beginnersbook.com; import java.util.ArrayList; public class ExampleOfaddAllMethod { public static void main(String[] args) { // ArrayList1 ArrayList<String> al = new ArrayList<String>(); al.add("Apple"); al.add("Orange"); al.add("Grapes"); al.add("Mango"); System.out.println("ArrayList1 before addAll:"+al); //ArrayList2 ArrayList<String> al2 = new ArrayList<String>(); al2.add("Fig"); al2.add("Pear"); al2.add("Banana"); al2.add("Guava"); System.out.println("ArrayList2 content:"+al2); //Adding ArrayList2 in ArrayList1 at 3rd position(index =2) al.addAll(2, al2); System.out.println("ArrayList1 after adding ArrayList2 at 3rd Pos:\n"+al); } } ``` 输出: ```java ArrayList1 before addAll:[Apple, Orange, Grapes, Mango] ArrayList2 content:[Fig, Pear, Banana, Guava] ArrayList1 after adding ArrayList2 at 3rd Pos: [Apple, Orange, Fig, Pear, Banana, Guava, Grapes, Mango] ``` #### 参考: [`addAll(int, Collection)`](https://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#addAll(int, java.util.Collection))