-
remove(int index)
The java.util.vector.remove(int index) method is used to remove an element from a Vector from a specific position or index.
Syntax:
Vector.remove(int index)
Parameters: This method accepts a mandatory parameter index is of integer data type and specifies the position of the element to be removed from the Vector.
Return Value: This method returns the element that has just been removed from the vector.
Below program illustrate the Java.util.Vector.remove(int index) method:
// Java code to illustrate remove() when position of// element is passed as parameterimportjava.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 VectorSystem.out.println("Vector: "+ vec_tor);// Remove the element using remove()String rem_ele = vec_tor.remove(4);// Print the removed elementSystem.out.println("Removed element: "+ rem_ele);// Print the final VectorSystem.out.println("Final Vector: "+ vec_tor);}}chevron_rightfilter_noneOutput:Vector: [Geeks, for, Geeks, 10, 20] Removed element: 20 Final Vector: [Geeks, for, Geeks, 10]
-
remove(Object o)
The java.util.vector.remove(Object o) method is used to remove any particular element from the Vector.
Syntax:
Vector.remove(Object o)
Parameters: This method accepts a mandatory parameter o is of the object type of Vector and specifies the element to be removed from the Vector.
Return Value: Returns True if the specified element is found and removed from the Vector, else False.
Below program illustrate the Java.util.Vector.remove(Object O) method:
// Java code to illustrate remove() methodimportjava.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 VectorSystem.out.println("Vector: "+ vec_tor);// Remove the head using remove()Boolean rem_ele;rem_ele = vec_tor.remove("Geeks");// Print the removed elementif(rem_ele)System.out.println("Geeks"+" found and removed.");elseSystem.out.println("Geeks"+" not found or removed.");rem_ele = vec_tor.remove("500");// Print the removed elementif(rem_ele)System.out.println("500"+" found and removed.");elseSystem.out.println("500"+" not found or removed.");// Print the final VectorSystem.out.println("Final Vector: "+ vec_tor);}}chevron_rightfilter_noneOutput:Vector: [Geeks, for, Geeks, 10, 20] Geeks found and removed. 500 not found or removed. Final Vector: [for, Geeks, 10, 20]
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Vector clear() Method in Java
- Vector addAll() Method in Java
- Vector set() Method in Java
- Vector isEmpty() Method in Java
- Vector lastIndexOf() Method in Java
- Vector addElement() Method in Java
- Vector subList() Method in Java
- Vector add() Method in Java
- Vector capacity() Method in Java
- Vector contains() Method in Java
- Vector containsAll() Method in Java
- Vector copyInto() Method in Java
- Vector indexOf() Method in Java
- Vector get() Method in Java
- Vector elementAt() Method in Java
- Vector elements() Method in Java
- Vector equals() Method in Java
- Vector size() Method in Java
- Vector hashCode() Method in Java
- Vector insertElementAt() 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.

