Vector add() Method in Java
- boolean add(Object element): This method appends the specified element to the end of this vector.
Syntax:
boolean add(Object element)
Parameters: This function accepts a single parameter element as shown in the above syntax. The element specified by this parameter is appended to end of the vector.
Return Value: This method returns True after successful execution, else False.
Below program illustrates the working of java.util.Vector.add(Object element) method:
Example:
// Java code to illustrate boolean add(Object element)importjava.util.*;publicclassVectorDemo {publicstaticvoidmain(String args[]){// Creating an empty VectorVector<String> vec_tor =newVector<String>();// Use add() method to add elements in the vectorvec_tor.add("Geeks");vec_tor.add("for");vec_tor.add("Geeks");vec_tor.add("10");vec_tor.add("20");// Output the present vectorSystem.out.println("The vector is: "+ vec_tor);// Adding new elements to the endvec_tor.add("Last");vec_tor.add("Element");// Printing the new vectorSystem.out.println("The new Vector is: "+ vec_tor);}}chevron_rightfilter_noneOutput:The vector is: [Geeks, for, Geeks, 10, 20] The new Vector is: [Geeks, for, Geeks, 10, 20, Last, Element]
- void add(int index, Object element): This method inserts an element at a specified index in the vector. It shifts the element currently at that position (if any) and any subsequent elements to the right (will change their indices by adding one).
Syntax:
void add(int index, Object element)
Parameters: This method accepts two parameters as described below.
- index: The index at which the specified element is to be inserted.
- element: The element which is needed to be inserted.
Return Value: This method does not return any value.
Exception: The method throws IndexOutOfBoundsException if the specified index is out of range of the size of the vector.
Below program illustrates the working of java.util.Vector.add(int index, Object element) method:
Example:
// Java code to illustrate boolean add(Object element)importjava.util.*;publicclassVectorDemo {publicstaticvoidmain(String args[]){// Creating an empty VectorVector<String> vec_tor =newVector<String>();// Use add() method to add elements in the vectorvec_tor.add("Geeks");vec_tor.add("for");vec_tor.add("Geeks");vec_tor.add("10");vec_tor.add("20");// Output the present vectorSystem.out.println("The vector is: "+ vec_tor);// Adding new elements to the endvec_tor.add(2,"Last");vec_tor.add(4,"Element");// Printing the new vectorSystem.out.println("The new Vector is: "+ vec_tor);}}chevron_rightfilter_noneOutput:The vector is: [Geeks, for, Geeks, 10, 20] The new Vector is: [Geeks, for, Last, Geeks, Element, 10, 20]
Recommended Posts:
- Vector set() Method in Java
- Vector contains() Method in Java
- Vector get() Method in Java
- Vector elementAt() Method in Java
- Vector removeElementAt() Method in Java
- Vector size() Method in Java
- Vector equals() Method in Java
- Vector elements() Method in Java
- Vector hashCode() Method in Java
- Vector remove() Method in Java
- Vector removeAll() Method in Java
- Vector lastElement() Method in Java
- Vector firstElement() Method in Java
- Vector insertElementAt() Method in Java
- Vector indexOf() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



