In this section, we will learn what the Vector add(), addAll(), addElement() methods are and how to use them in Java.
What is Java Vector add() Method?
The Java Vector add() method is used to add a new element to a vector object.
Java Vector add() Method Syntax:
public boolean add(E e)
add() Method Parameters:
The method takes one argument and that is the element we want to add to a vector object.
add() Method Return Value:
The return value of this method is of Boolean type.
If the element was added to the target vector object, then the return value of this method becomes true. Otherwise, we get the value false instead.
add() Method Exceptions:
ClassCastException: we get this exception if the element set as the argument of the method is of incompatible type compared to what the vector object accepts.
Example: using Vector add() method
import java.util.Vector; import java.util.ArrayList; import java.util.Enumeration; public class Main { public static void main(String[] args){ Vector <Integer> vector = new Vector<>(); vector.add(1); vector.add(2); vector.add(3); vector.add(4); vector.add(5); vector.add(6); vector.add(7); vector.add(8); vector.add(9); System.out.println(vector.add(1)); } }
Output:
true
What is Java Vector addAll() Method?
The Java Vector addAll() method is used to add more than one element to a vector object.
Basically, using this method, we can take the elements of a collection and put it as the elements of a vector object.
Java Vector addAll() Method Syntax:
public boolean addAll(Collection<? extends E> c)
addAll() Method Parameters:
The method takes one argument and that is a reference to a collection object that we want to copy its elements and put it in the target vector object.
addAll() Method Return Value:
The return value of this method is of Boolean type.
If the elements of the target collection were copied into the target vector object, then the result of this method becomes true.
Otherwise, the value false will return instead.
addAll() Method Exceptions:
The method might throw one exception and that is:
NullPointerException: we get this exception if we set the value null as the argument of this method.
Example: using Vector addAll() method
import java.util.Vector; import java.util.ArrayList; public class Main { public static void main(String[] args){ Vector <Integer> vector = new Vector<>(); vector.add(1); vector.add(2); vector.add(3); vector.add(4); vector.add(5); ArrayList<Integer> list = new ArrayList<>(); list.add(6); list.add(7); list.add(8); vector.addAll(list); for (int i: vector){ System.out.print(", "+i); } } }
Output:
, 1, 2, 3, 4, 5, 6, 7, 8
What is Java Vector addElement() Method?
The Java Vector addElement() method is used to add a new element to the end of a vector object, which increases its size by one.
Note: this method acts the same as the add() method you saw at the beginning of this section. Except this method does not return a value as a result of calling it.
Java Vector addElement() Method Syntax:
public void addElement(E obj)
addElement() Method Parameters:
The method takes one argument and that is the element that we want to add to a vector object.
addElement() Method Return Value:
The return value of this method is void.
addElement() Method Exceptions:
The method throws the same exception as the add() method.
Example: using Vector addElement() method
import java.util.Vector; public class Main { public static void main(String[] args){ Vector <Integer> vector = new Vector<>(); vector.addElement(1); vector.addElement(2); vector.addElement(3); vector.addElement(4); vector.addElement(5); } }