Java Vector add() Method24 Mar 2025 | 3 min read The add() is a Java Vector class method which is used to insert the specified element in the given Vector. There are two different types of Java add() method which can be differentiated depending on its parameter. These are:
Java Vector add(int index, E element)This method is used to insert the specified element at the specified position in the given Vector. Java Vector add(E e)This method appends the specified element to the end of this vector. SyntaxFollowing is the declaration of add() method: Parameter
ReturnThe add(int index, E element) method does not return anything because its return type is void. The add(E e) method returns true if the specified element added successfully, otherwise returns false. ExceptionsArrayIndexOutOfBoundsException- This method has thrown an exception if the index is out of range. Compatibility VersionJava 1.2 and above Example 1Output: --Elements of Vector are-- Alphabet= A Alphabet= B Alphabet= C Alphabet= D Alphabet= E Example 2Output: Vector is: [Java, Android, Python, tpointtech, Hindi100] Example 3Output: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 > 3 at java.base/java.util.Vector.insertElementAt(Vector.java:619) at java.base/java.util.Vector.add(Vector.java:857) at myPackage.VectorAddExample3.main(VectorAddExample3.java:11) Example 4Output: Element at index: 0 Color: White Element at index: 1 Color: Green Element at index: 2 Color: Black Element at index: 3 Color: Pink New color Yellow added at first position. Element at index: 0 Color: White Element at index: 1 Color: Yellow Element at index: 2 Color: Green Element at index: 3 Color: Black Element at index: 4 Color: Pink Next TopicJava Vector |
Java Method The removeAll() method of Java Vector class deletes all the elements from the vector that are present in the specified collection. Syntax Following is the declaration of removeAll() method: public boolean removeAll(Collection<?> c) Parameter Parameter Description Required/Optional c It is a collection of elements which will be removed from the vector. Required Return The removeAll()...
3 min read
Java Method The set() method of Java Vector class is used to replace the element at the specified position in the vector with the specified element. Syntax Following is the declaration of set() method: Public E set(int index, E element) Parameter Parameter Description Required/Optional index It is an index of the element which will...
2 min read
Java Method The toString() method of Java Vector class is used to get a string representation of the vector. It contains the string representation of each element. Syntax Following is the declaration of toString() method: public String toString() Parameter This method does not accept any parameter. Return The toString() method returns a...
2 min read
Java Method The removeAllElements() method of Java Vector class is used to remove all elements from the vector and set the size of the vector to zero. Syntax Following is the declaration of removeAllElements() method: Public void removeAllElements() Parameter This method does not accept any parameter. Return This method has return type...
2 min read
Java Method The lastIndexOf() Java Vector class method is used to get the index of the last occurrence of the specified element in the vector. There are two different types of Java lastIndexOf() method which can be differentiated depending on its parameter. These are: Java Vector...
3 min read
Java Method The lastElement() method of Java Vector class is used to get the last component of the vector. Syntax Following is the declaration of lastElement() method: Public E lastElement() Parameter This method does not accept any parameter. Return The lastElement() method returns the last element of the vector i.e. the component...
2 min read
Java Method The clear() method of Java Vector class is used to remove all of the elements from the vector which is in use. Syntax: Following is the declaration of clear() method: Public void clear() Parameter: This method does not accept any parameter. Returns: This method has return type void so, it...
2 min read
Java Method The setSize() method of Java Vector class is used to set the size of a vector. If the new size is greater than the current size, null items are added to the end of the vector. Otherwise, all components at index newSize and...
3 min read
Java Method The capacity() method of Java Vector class is used to get the current capacity of the vector which is in use. Syntax: Following is the declaration of capacity() method: Public int capacity() Parameter: This method does not accept any parameter. Returns: This method returns the current capacity of the vector...
2 min read
Java Method The ensureCapacity() method of Java Vector class is used to increase the capacity of the vector which is in use, if necessary. It ensures that the vector can hold at least the number of components specified by the minimum capacity argument. Syntax: Following is the...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India