Java arraylist addall – Java ArrayList addAll() Method with Example

Java arraylist addall: In this article we are going to see the use ArrayList  addAll() method along with suitable examples by using Java programming language.

Java ArrayList addAll() Method with Example

Let’s see addAll() method with suitable examples.

addAll(Collection col):

Arraylist addall: This java.util.ArrayList.addAll(Collection col) method adds/insert all the element of a collection to the arraylist and returns True if it successfully adds all the elements of collection to the arraylist. NullPointerException will come if the collection that you want to add does not have any element in it.

Syntax:

arrayListName.addAll(Collection col)

Where,

  • arrayListName refers to the name of your ArrayList.
  • Collection col refers to another arraylist that will be added into the arrayList.

Example: Java ArrayList addAll() Method – Example with Integer Type ArrayList

Approach:

  • Create a new ArrayList of type Integer.
  • Add Integer elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Add another new element to the new ArrayList using add( ) method.
  • Print the new ArrayList. With the previous arrayList using addAll(Collection col) method.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a ArrayList of string datatype
        ArrayList<Integer> arr1 = new ArrayList<Integer>();
        // Adding some elements to the ArrayList
        arr1.add(1);
        arr1.add(2);
        arr1.add(3);
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList 1 are: "+arr1);
        // Create a ArrayList of string datatype
        ArrayList<Integer> arr2 = new ArrayList<Integer>();
        // Adding some elements to the ArrayList
        arr2.add(4);
        arr2.add(5);
        arr2.add(6);
        // Prints the new ArrayList elements
        System.out.println("The elements of ArrayList 2 are: "+arr2);
        // Adding both arrayList 1 and 2 in a single arrayList using addAll() method
        arr1.addAll(arr2);
        // Prints the new ArrayList elements
        System.out.println("The new elements of ArrayList 1 are: "+arr1);
    }
}
Output:

The elements of ArrayList 1 are: [1, 2, 3]
The elements of ArrayList 2 are: [4, 5, 6]
The new elements of ArrayList 1 are: [1, 2, 3, 4, 5, 6]

addAll(int index, Collection col):

Addall java: This java.util.ArrayList.addAll(int index, Collection col) method adds/insert all the element of a collection to the arraylist starting from the specified index position of the arraylist and returns True if it successfully adds all the elements of collection to the arraylist. NullPointerException will come if the collection that you want to add does not have any element in it..

Syntax:

arrayListName.addAll(int index, Collection col)

Where,

  • arrayListName refers to the name of your ArrayList.
  • Collection col refers to another arraylist that will be added into the arrayList.
  • int index refers to the arraylist index position from where new collection elements will be added.

Example: Java ArrayList addAll() Method – Example with Integer Type ArrayList

  • Create a new ArrayList of type Integer.
  • Add Integer elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Add another new element to the new ArrayList using add( ) method.
  • Display the ArrayList elements.
  • Print the new ArrayList. With the previous arrayList using addAll(int index, Collection col) method to a specified position.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a ArrayList of string datatype
        ArrayList<Integer> arr1 = new ArrayList<Integer>();
        // Adding some elements to the ArrayList
        arr1.add(1);
        arr1.add(2);
        arr1.add(3);
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList 1 are: "+arr1);
        // Create a ArrayList of string datatype
        ArrayList<Integer> arr2 = new ArrayList<Integer>();
        // Adding some elements to the ArrayList
        arr2.add(4);
        arr2.add(5);
        arr2.add(6);
        // Prints the new ArrayList elements
        System.out.println("The elements of ArrayList 2 are: "+arr2);
        // Adding both arrayList 1 and 2 in a single arrayList using addAll() method
        arr1.addAll(2,arr2);
        // Prints the new ArrayList elements
        System.out.println("The new elements of ArrayList 1 are: "+arr1);
    }
}
Output:

The elements of ArrayList 1 are: [1, 2, 3]
The elements of ArrayList 2 are: [4, 5, 6]
The new elements of ArrayList 1 are: [1, 2, 4, 5, 6, 3]

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: