The java.util.ArrayList.addAll(Collection<? extends E> c) method 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).
The following example shows the usage of java.util.ArrayList.addAll(c) method:
import java.util.ArrayList; public class ArrayListDemo { public static void main(String args[]) { // create an empty array list1 with an initial capacity ArrayList<Integer> arrlist = new ArrayList<Integer>(5); // use add() method to add elements in the list arrlist.add(12); arrlist.add(20); arrlist.add(45); // let us print all the elements available in list1 System.out.println("Printing list1:"); for (Integer number : arrlist) { System.out.println("Number = " + number); } // create an empty array list2 with an initial capacity ArrayList<Integer> arrlist2 = new ArrayList<Integer>(5); // use add() method to add elements in list2 arrlist2.add(25); arrlist2.add(30); arrlist2.add(31); arrlist2.add(35); // let us print all the elements available in list2 System.out.println("Printing list2:"); for (Integer number : arrlist2) { System.out.println("Number = " + number); } // inserting all elements, list2 will get printed after list1 arrlist.addAll(arrlist2); System.out.println("Printing all the elements"); // let us print all the elements available in list1 for (Integer number : arrlist) { System.out.println("Number = " + number); } } }Let us compile and run the above program, this will produce the following result −
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
Java ArrayList Source Code Examples
- Sort List of Integers in Ascending and Descending Order Example
- List (ArrayList) Iterator Example
- Create the Immutable ArrayList with List.of() Method Example
- Create Immutable ArrayList with Collections.unmodifiableList() Exmaple
- Java 10 - Copy List into Another List Exmaple
- Java 8 - Copy List into Another List Example
- Java - Copy a List to Another List using Collections.copy() method
- Java - Copy a List to Another List Example
- Java ArrayList spliterator() Method Example
- Java ArrayList sort() Method Example
- Java ArrayList retainAll() Method Example
- Java ArrayList removeIf() Method Example
- Java ArrayList removeAll() Method Example
- Java ArrayList remove() Method Example
- Java ArrayList lastIndexOf() Method Example
- Java ArrayList isEmpty() Method Example
- Java util ArrayList indexOf() Method Example
- Java ArrayList get() Method Example
- Java ArrayList ensureCapacity() Method Example
- Java ArrayList contains() Method Example
- Java ArrayList clone() Method Example
- Java ArrayList clear() Method Example
- Java ArrayList addAll() Method Example
- Java ArrayList add() Method Example
- Java 8 forEach() List Example
- Add Enum Values to ArrayList Example
- Join List Strings with Commas in Java
- Java Stream filter null values example
- Java ArrayList subList() Example
- Get Index of Elements in ArrayList Example
- Java ArrayList removeIf() Example
- Java ArrayList add(), get() and set() Method Example
- Iterate over ArrayList Using forEach Java
- Iterate over ArrayList using Iterator in Java
- Java ArrayList indexOf() and lastIndexOf() Example
- Search an Element in an ArrayList in Java
- Clear ArrayList in Java Example
- Java ArrayList removeAll() Method Example
- Java ArrayList remove() Method Example
- How to Iterate over ArrayList in Java
- How to Remove Element from ArrayList in Java
- How to Access Elements of ArrayList in Java
- Create ArrayList from Another ArrayList in Java
- How to Create an ArrayList and Add Elements to It
Comments
Post a Comment