Java Vector addAll() Method



Description

The Java Vector addAll(Collection<? extends E> c) method is used to append all of the elements in the specified collection to the end of the list.The order will be same as they are returned by the specified collection's Iterator. This method should not be called at the same time while modifying the collection.This will cause undefined behavior and incorrect result.

Declaration

Following is the declaration for java.util.Vector.addAll() method

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

Parameters

c − This is the collection containing elements to be added to this list.

Return Value

The return type is true if this list is changed as a result of the call.

Exception

NullPointerException − The method call will throw this exception if the specified collection is null.

Java Vector addAll(int index,Collection<? extends E> c) Method

Description

This Java Vector addAll(int index,Collection<? extends E> c) method is another variant of the previous addAll() method. It inserts all of the elements in the specified collection into this list. The insertion starts at the specified position/index and shifts the element currently at that position (if any).The corresponding indices are also increased to shift subsequent elements to the right.The new elements will appear in the list in the same order as they are returned by the specified collection's iterator.

Declaration

Following is the declaration for java.util.Vector.addAll() method

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

Parameters

  • index − This is the index/position where the first element from the specified collection will be inserted.

  • c − This is the collection containing elements to be added to this list .

Return Value

The return type is true if this list is changed as a result of the call.

Exception

  • NullPointerException − The method call will throw this exception if the specified collection is null.

  • IndexOutOfBoundsException − This exception will be thrown if the accessed index/position is out of range (index < 0 || index > size()).

Adding Multiple Elements to a Vector of Integer Example

The following example shows the usage of Java Vector addAll(c) method to add Integers. We're adding couple of Integers to the Vector object using addAll() method in single statement and then print each element to show the elements added.

package com.tutorialspoint;

import java.util.Vector;
import java.util.Arrays;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty array list 
      Vector<Integer> vector = new Vector<>();

      // use addAll() method to add elements in the vector
      vector.addAll(Arrays.asList(10,20,30,40,50));

      // let us print all the elements available in vector
      System.out.println("Vector = " + vector);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Vector = [10, 20, 30, 40, 50]

Adding multiple Elements to a Vector of String Example

The following example shows the usage of Java Vector addAll(c) method to add Strings. We're adding couple of Strings to the Vector object using addAll() method in single statement and then print each element to show the elements added.

package com.tutorialspoint;

import java.util.Vector;
import java.util.Arrays;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty array list 
      Vector<String> vector = new Vector<>();

      // use addAll() method to add elements in the vector
      vector.addAll(Arrays.asList("A","B","C","D","E"));

      // let us print all the elements available in vector
      System.out.println("Vector = " + vector);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Vector = [A, B, C, D, E]

Adding multiple Elements to a Vector of Objects at Particular Index Example

The following example shows the usage of Java Vector addAll(index, c) method to add a collection of Student objects at particular index. We're adding couple of Student objects to the Vector object using add() method calls per element and using addAll(index, E) in the end to add few students at particular location and then printing the Vector using its toString() method.

package com.tutorialspoint;

import java.util.Vector;
import java.util.Arrays;

public class VectorDemo {
   public static void main(String[] args) {

      // create an empty vector
      Vector<Student> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add(new Student(1, "Julie"));
      vector.add(new Student(2, "Robert"));
      vector.addAll(0, Arrays.asList(new Student(3, "Adam"), new Student(4, "Jene")));
      System.out.println("Vector = " + vector);      
   }
}

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Vector = [[ 3, Adam ], [ 4, Jene ], [ 1, Julie ], [ 2, Robert ]]
java_util_vector.htm
Advertisements