addAll(Coll C) 方法在 Java 中有何作用?
类java.util.ArrayList 的addAll(Collection<? extends E> c) 方法把指定集合中的所有元素追加到此列表的末尾,按照它们由指定集合的 Iterator 返回的顺序。如果在操作进行期间对指定的集合进行了修改,则此操作的行为是未定义的(这意味着如果指定的集合是此列表且此列表不为空,则此调用的行为是未定义的)。
示例
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("Printing list1:"); 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("Printing list2:"); for (Integer number : arrlist2) { System.out.println("Number = " + number); } arrlist.addAll(arrlist2); System.out.println("Printing all the elements"); for (Integer number : arrlist) { System.out.println("Number = " + number); } } }
输出
Printing list1: Number = 12 Number = 20 Number = 45 Printing list2: Number = 25 Number = 30 Number = 31 Number = 35 Printing all the elements Number = 12 Number = 20 Number = 45 Number = 25 Number = 30 Number = 31 Number = 35
广告