Java Vector remove() Method24 Mar 2025 | 3 min read The remove() Java Vector class method is used to remove the specified element from the vector. There are two different types of Java remove() method which can be differentiated depending on its parameter. These are:
remove(int index) Method:This method remove the element at the specified position in this vector. remove(Object o) Method:This method remove the first occurrence of the specified element in this vector. If the vector does not contain the element, it is unchanged. SyntaxFollowing is the declaration of remove() method: Parameter
ReturnThe remove(int index) method returns the element that was removed. The remove(Object obj) method returns true if the vector contained the specified element. ExceptionsArrayIndexOutOfBoundsException- This method has thrown an exception if the index is out of range i.e. (index < 0 || index >= size()). Compatibility VersionJava 1.2 and above Example 1Output: Values in vector: [100, 200, 300, 200, 400] Remove first occourence of element 200: true Values in vector: [100, 300, 200, 400] Example 2Output: Elements of the Vector before remove: [Java, Android, JavaScript, Java] Is the removal successful? true Elements of the Vector after remove: [Android, JavaScript, Java] Example 3Output: Values in vector :[3, 5, 3, 4] Removed element at index 2 is: 3 Values in vector after remove: [3, 5, 4] Example 4Output: Values in vector :[Facebook, Whatsapp, Twitter, Instagram, Skype] Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2 at java.base/java.util.Vector.elementData(Vector.java:761) at java.base/java.util.Vector.remove(Vector.java:875) at myPackage.VectorRemoveExample4.main(VectorRemoveExample4.java:16) Next TopicJava Vector |
Java Method The removeRange() method of Java Vector class is used to delete all elements from the vector whose index is between fromIndex, inclusive and toIndex, exclusive. The removal of elements shifts any succeeding elements to the left (reduces their index). Syntax Following is the declaration of...
2 min read
Java Method The addElement() method of Java Vector class is used to add the specified element to the end of this vector. Adding an element increases the vector size by one. Syntax: Following is the declaration of addElement() method: Public void addElement(E e) Parameter: Parameter Description Required/Optional e It is the element which will...
2 min read
Java Method The get() method of Java Vector class is used to get the element at the specified position in the vector. Syntax Following is the declaration of get() method: public E get(int index) Parameter DataType Parameter Description Required/Optional int index It is an index of the element which will be returned. Required Return The get() method returns the...
2 min read
Java Method The elementAt() method of Java Vector class is used to get the element at the specified index in the vector. Syntax Following is the declaration of elementAt() method: public E elementAt(int index) Parameter DataType Parameter Description Required/Optional int index It is an index into this vector. Required Return The elementAt() method returns an element at the specified...
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 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 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 hashCode() method of Java Vector class is used to get the hashcode value for the vector which is in use. Syntax: Following is the declaration of hashCode() method: public int hashCode() Parameter: This method does not accept any parameter. Returns: The hashCode() method returns the hash code value for...
2 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
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