方法addAll(int,Coll C)在Java中做什么?
类 java.util.ArrayList的 addAll(intindex,Collection<?扩展E>c)方法从指定位置开始将指定集合中的所有元素插入此列表。它将当前位于该位置的元素(如果有)和任何后续元素右移(增加其索引)。新元素将按照指定集合的迭代器返回的顺序显示在列表中。
示例
import java.util.ArrayList; public class ArrayListDemo { public static void main(String args[]) { ArrayList<Integer> arrlist = new ArrayList<Integer>(5); arrlist.add(12); arrlist.add(20); arrlist.add(45); System.out.println("打印列表1:"); for (Integer number : arrlist) { System.out.println("Number = " + number); } ArrayList<Integer> arrlist2 = new ArrayList<Integer>(5); arrlist2.add(25); arrlist2.add(30); arrlist2.add(31); arrlist2.add(35); System.out.println("打印列表2:"); for (Integer number : arrlist2) { System.out.println("Number = " + number); } arrlist.addAll(2, arrlist2); System.out.println("Printing all the elements"); for (Integer number : arrlist) { System.out.println("Number = " + number); } } }
输出结果
打印列表1: Number = 12 Number = 20 Number = 45 打印列表2: Number = 25 Number = 30 Number = 31 Number = 35 Printing all the elements Number = 12 Number = 20 Number = 25 Number = 30 Number = 31 Number = 35 Number = 45