What does the method addAll(Coll C)do in java?

Java 8Object Oriented ProgrammingProgramming

The addAll(Collection<? extends E> c) method of the class java.util.ArrayList appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress (implies that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty).

Example

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);
      }
   }
}

Output

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
raja
Published on 16-Jan-2018 11:43:38
Advertisements