Java vector addAll method explanation with example

Java vector addAll method explanation with example:

The addAll method of the Java vector class is used to append all items of a collection to a vector. This is a very useful method and instead of iterating over a collection and appending the elements to a vector, we can use this method.

There are two overloaded methods defined in the java.util.Vector class.

public boolean addAll(Collection<? extends E> c):

This method is defined in the java.util.Vector class. It takes one collection as the argument and appends all of the elements of this collection to the end of the vector where this method is called.

It takes only one parameter, c, which are the elements to insert to the vector. It returns one boolean value. It returns true if the vector is modified after this method call. Else, it returns false.

It might return NullPointerException if the parameter is null. Also, the data type of the collection and vector shouldn’t be different.

Let’s try this method with an example:

import java.util.ArrayList;
import java.util.Vector;

class Main {

    public static void main(String[] args) {
        Vector<Integer> intVector = new Vector<>();
        intVector.add(1);
        intVector.add(2);

        ArrayList<Integer> intList = new ArrayList<>();
        intList.add(3);
        intList.add(4);
        intList.add(5);
        intList.add(6);

        intVector.addAll(intList);

        System.out.println("Vector content: ");
        for (int i : intVector) {
            System.out.print(i+" ");
        }
    }
}

Here,

  • We created an integer vector, intVector and two integers are added to the vector.
  • Another ArrayList is created and four integers are added to the list.
  • With the addAll method, the integer ArrayList is appended to the integer Vector.
  • The last for loop is printing the content of the integer vector.

If you run the above program, it will print:

Vector content: 
1 2 3 4 5 6 

Example of addAll with a null parameter:

Let’s try addAll with a null parameter value. It will throw a NullPointerException.

import java.util.Vector;

class Main {

    public static void main(String[] args) {
        Vector<Integer> intVector = new Vector<>();
        intVector.add(1);
        intVector.add(2);

        intVector.addAll(null);

        System.out.println("Vector content: ");
        for (int i : intVector) {
            System.out.print(i+" ");
        }
    }
}

If you run this program, it will throw a NullPointerException because we are passing null to the addAll method.

NullPointerException addAll method

public synchronized boolean addAll(int index, Collection<? extends E> c):

This is the overloaded method of addAll. It is used to insert all items from a given collection c into the vector starting from the given index. It shifts all the elements of the vector to the right.

The first parameter index is the index at which the first element of the collection, c, is to be inserted in the vector.

It returns one boolean value, true if the result is successful and false otherwise.

It will throw NullPointerException if the collection is null or ArrayIndexOutOfBoundsException if the index is invalid i.e. if it is smaller than 0 or greater than the size of the vector.

Below is the complete program:

import java.util.ArrayList;
import java.util.Vector;

class Main {

    public static void main(String[] args) {
        Vector<Integer> intVector = new Vector<>();
        intVector.add(1);
        intVector.add(2);
        intVector.add(3);
        intVector.add(4);
        intVector.add(5);

        ArrayList<Integer> intList = new ArrayList<>();
        intList.add(-1);
        intList.add(-2);
        intList.add(-3);
        intList.add(-4);

        intVector.addAll(2, intList);

        System.out.println("Vector content: ");
        for (int i : intVector) {
            System.out.print(i + " ");
        }
    }
}

Here,

  • The intVector is the integer vector where we are appending the content of intList, which is an ArrayList.
  • It inserts the items at index 2.

If you run this program, it will print the below output:

Vector content: 
1 2 -1 -2 -3 -4 3 4 5 

As you can see here, the items are inserted from the second index and other elements are pushed to the right.

You might also like: