Home » Java programming language

Java ArrayList addAll() Method with Example

ArrayList Class addAll() method: Here, we are going to learn about the addAll() method of ArrayList Class with its syntax and example.
Submitted by Preeti Jain, on January 18, 2020

ArrayList Class addAll() method

Syntax:

    public boolean addAll(Collection cl);
    public boolean addAll(int indices, Collection cl);
  • addAll() method is available in java.util package.
  • addAll(Collection cl) method is used to add all the elements of the given collection to the last of this Arraylist in the same sequence as they are retrieved by the given object Iterator.
  • addAll(int indices, Collection cl) method is used to add all the elements of the given collection into this Arraylist and inserting position start from the given indices.
  • addAll(Collection cl) method may throw an exception at the time of converting given collection to Arraylist.
    NullPointerException: This exception may throw when the given collection is null exists.
  • addAll(int indices, Collection cl) method may throw an exception at the time of conversion of given collection to this Arraylist.
    • IndexOutOfBoundsException: This exception may throw when the given parameter indices are not in a range.
    • NullPointerException: This exception may throw when the given parameter collection if null exists.
  • These are non-static methods, so it is accessible with the class object and if we try to access the method with the class name then we will get an error.

Parameter(s):

  • In the first case, addAll(Collection cl)
    • Collection cl – represents the collection object that contains elements to be inserted in this Arraylist.
  • In the second case, addAll(int indices, Collection cl)
    • int indices – represent the starting index to place the element of the given collection.
    • Collection cl – represent the Collection object that contains elements to be inserted in this Arraylist.

Return value:

In the first case, the return type of the method is boolean, it returns true if the given collection is added to this Arraylist.

In the second case, the return type of the method is boolean, it returns true if the given collection is added from the given indices.

Example:

// Java program to demonstrate the example 
// of void addAll() method of ArrayList

import java.util.*;

public class AddAllOfArrayList {
    public static void main(String args[]) {
        // Create an ArrayList 1 , 2 with initial capacity
        // to store elements
        ArrayList < String > arr_l1 = new ArrayList < String > (10);
        ArrayList < String > arr_l2 = new ArrayList < String > (10);

        // By using add() method is to add elements
        // in the ArrayList
        arr_l1.add("C");
        arr_l1.add("C++");
        arr_l1.add("Java");
        arr_l1.add("DotNet");

        arr_l2.add("OS");
        arr_l2.add("DBMS");

        // Display ArrayList 1
        System.out.println("ArrayList 1 Elements :" + arr_l1);

        // Display ArrayList 2
        System.out.println("ArrayList 2 Elements :" + arr_l2);

        // By using addAll(Collection) method is to add all the 
        // elements in the given collection
        arr_l1.addAll(arr_l2);

        // Display ArrayList
        System.out.println("arr_l1.addAll(arr_l2) : " + arr_l1);

        // By using addAll(int, Collection) method is to add all the 
        // elements of arr_l2 at index 1 in arr_l1
        arr_l1.addAll(1, arr_l2);

        // Display ArrayList
        System.out.println("arr_l1.addAll(1,arr_l2) : " + arr_l1);
    }
}

Output

ArrayList 1 Elements :[C, C++, Java, DotNet]
ArrayList 2 Elements :[OS, DBMS]
arr_l1.addAll(arr_l2) : [C, C++, Java, DotNet, OS, DBMS]
arr_l1.addAll(1,arr_l2) : [C, OS, DBMS, C++, Java, DotNet, OS, DBMS]



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.