This example shows how to add elements to Vector in Java. This example also shows how to add or insert elements to Vector using the add, addElement, and addAll methods.
How to add elements to Vector in Java?
How to add an element to a Vector object using the add and addElement methods?
The Vector add
method adds the specified element to this vector object at the end.
1 |
public boolean add(E element) |
The add
method always returns true as specified by the add
method of the Collection interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Vector; public class JavaVectorAddElementsExample { public static void main(String[] args) { Vector<Integer> vNumbers = new Vector<Integer>(); //this will add element "1" and return true System.out.println( vNumbers.add(1) ); //this will add element "2" and return true System.out.println( vNumbers.add(2) ); System.out.println("Vector contains: " + vNumbers); } } |
Output
1 2 3 |
true true Vector contains: [1, 2] |
The Vector addElement
method is identical to the add
method in functionality.
1 |
public void addElement(E object) |
It appends the specified element at the end of the vector object. However, there is a difference in the return types of add
and addElement
methods. The add
method returns a boolean value while the return type of the addElement
method is void.
How to insert elements in the Vector at the specified index?
The overloaded add
method accepts an index parameter.
1 |
public void add(int index, E element) |
It inserts the specified element at the specified index in the vector object. It shifts the subsequent elements to the right by adding 1 to their indices.
1 2 3 4 5 6 7 8 9 10 11 12 |
Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(1); vNumbers.add(2); vNumbers.add(3); System.out.println("Vector contains: " + vNumbers); //this will insert element at index 1 vNumbers.add(1, 100); System.out.println("Vector contains: " + vNumbers); |
Output
1 2 |
Vector contains: [1, 2, 3] Vector contains: [1, 100, 2, 3] |
The insertElementAt
method is similar to this overloaded add
method in functionality and inserts an element at the specified index of the vector object.
1 |
public void insertElementAt(E object, int index) |
However, there is a difference in the order of the parameters.
How to add all elements of another collection to the Vector using the addAll method?
The Vector addAll
method adds all elements of the specified collection object to this vector object.
1 |
public boolean addAll(Collection<? extends E> collection) |
It returns true if this vector is changed due to this method call.
The below given example shows how to add all elements of an ArrayList to a Vector object using the addAll
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
List<Integer> listNumbers = new ArrayList<Integer>(); listNumbers.add(10); listNumbers.add(20); listNumbers.add(30); Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(40); vNumbers.add(50); //this will add all elements of ArrayList to this Vector object vNumbers.addAll(listNumbers); System.out.println("Vector contains: " + vNumbers); |
Output
1 |
Vector contains: [40, 50, 10, 20, 30] |
The collection elements are appended at the end of this vector object.
If you want to insert the collection elements at the specific index instead of at the end, you can use the overloaded addAll
method that also accepts an index.
1 |
public boolean addAll(int index, Collection<? extends E> collection) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
List<Integer> listNumbers = new ArrayList<Integer>(); listNumbers.add(10); listNumbers.add(20); listNumbers.add(30); Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(40); vNumbers.add(50); /* * This will insert all elements of the ArrayList * to this vector object at index 1 */ vNumbers.addAll(1, listNumbers); System.out.println("Vector contains: " + vNumbers); |
Output
1 |
Vector contains: [40, 10, 20, 30, 50] |
This example is a part of the Vector in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Vector Documentation