Vector remove() Method in Java

26 Nov, 2018
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 parameterimport java.util.*;  public class VectorDemo {    public static void main(String args[])    {          // Creating an empty Vector        Vector<String> vec_tor = new Vector<String>();          // Use add() method to add elements in the Vector        vec_tor.add("Geeks");        vec_tor.add("for");        vec_tor.add("Geeks");        vec_tor.add("10");        vec_tor.add("20");          // Output the Vector        System.out.println("Vector: " + vec_tor);          // Remove the element using remove()        String rem_ele = vec_tor.remove(4);          // Print the removed element        System.out.println("Removed element: " + rem_ele);          // Print the final Vector        System.out.println("Final Vector: " + vec_tor);    }}Output:Vector: [Geeks, for, Geeks, 10, 20]
Removed element: 20
Final Vector: [Geeks, for, Geeks, 10]

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 parameterimport java.util.*;  public class VectorDemo {    public static void main(String args[])    {          // Creating an empty Vector        Vector<String> vec_tor = new Vector<String>();          // Use add() method to add elements in the Vector        vec_tor.add("Geeks");        vec_tor.add("for");        vec_tor.add("Geeks");        vec_tor.add("10");        vec_tor.add("20");          // Output the Vector        System.out.println("Vector: " + vec_tor);          // Remove the element using remove()        String rem_ele = vec_tor.remove(4);          // Print the removed element        System.out.println("Removed element: " + rem_ele);          // Print the final Vector        System.out.println("Final Vector: " + vec_tor);    }}
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() methodimport java.util.*;  public class VectorDemo {    public static void main(String args[])    {        // Creating an empty Vector        Vector<String> vec_tor = new Vector<String>();          // Use add() method to add elements in the Vector        vec_tor.add("Geeks");        vec_tor.add("for");        vec_tor.add("Geeks");        vec_tor.add("10");        vec_tor.add("20");          // Output the Vector        System.out.println("Vector: " + vec_tor);          // Remove the head using remove()        Boolean rem_ele;          rem_ele = vec_tor.remove("Geeks");        // Print the removed element        if (rem_ele)            System.out.println("Geeks"                               + " found and removed.");        else            System.out.println("Geeks"                               + " not found or removed.");          rem_ele = vec_tor.remove("500");        // Print the removed element        if (rem_ele)            System.out.println("500"                               + " found and removed.");        else            System.out.println("500"                               + " not found or removed.");          // Print the final Vector        System.out.println("Final Vector: " + vec_tor);    }}Output:Vector: [Geeks, for, Geeks, 10, 20]
Geeks found and removed.
500 not found or removed.
Final Vector: [for, Geeks, 10, 20]

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() methodimport java.util.*;  public class VectorDemo {    public static void main(String args[])    {        // Creating an empty Vector        Vector<String> vec_tor = new Vector<String>();          // Use add() method to add elements in the Vector        vec_tor.add("Geeks");        vec_tor.add("for");        vec_tor.add("Geeks");        vec_tor.add("10");        vec_tor.add("20");          // Output the Vector        System.out.println("Vector: " + vec_tor);          // Remove the head using remove()        Boolean rem_ele;          rem_ele = vec_tor.remove("Geeks");        // Print the removed element        if (rem_ele)            System.out.println("Geeks"                               + " found and removed.");        else            System.out.println("Geeks"                               + " not found or removed.");          rem_ele = vec_tor.remove("500");        // Print the removed element        if (rem_ele)            System.out.println("500"                               + " found and removed.");        else            System.out.println("500"                               + " not found or removed.");          // Print the final Vector        System.out.println("Final Vector: " + vec_tor);    }}
Vector: [Geeks, for, Geeks, 10, 20]
Geeks found and removed.
500 not found or removed.
Final Vector: [for, Geeks, 10, 20]

Java - util package
Java-Collections
Java-Functions
Java-Vector
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Arrays in Java
Split() String method in Java with examples
Arrays.sort() in Java with examples
Reverse a string in Java
Object Oriented Programming (OOPs) Concept in Java
For-each loop in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java