The Wayback Machine - https://web.archive.org/web/20240618020200/https://www.geeksforgeeks.org/vector-remove-method-in-java/
Open In App

Vector remove() Method in Java

Last Updated : 26 Nov, 2018
Improve
Improve
Like Article
Like
Save
Share
Report
  • 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 parameter
    import 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]
    
  • 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() method
    import 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]
    


Previous Article
Next Article

Similar Reads

Copy Elements of One Java Vector to Another Vector in Java
Vector is similar to arrays but is growable also, or we can say no fixed size is required. Previously vector was a part of legacy classes but now it is part of Collections. It also implements a List interface, so we can use any method of list interface on vectors also. Syntax : Vector&lt;Integer&gt; gfg=new Vector&lt;&gt;(); Ways To copy elements o
3 min read
How to Remove Duplicate Elements from the Vector in Java?
Using LinkedHashSet and TreeSet, duplicate elements are removed. Because the LinkedHashSet and TreeSet do not accept duplicate elements. Example: Input : vector = [1, 2, 3, 4, 2, 4] Output: vector = [1, 2, 3, 4] Input : vector = [a, b, a, c, d, a] Output: vector = [a, b, c, d]Approach 1: Using LinkedHashSet LinkedHashSet does not accept duplicate e
3 min read
Vector clear() Method in Java
The Java.util.Vector.clear() method is used to remove all the elements from a Vector. Using the clear() method only clears all the element from the vector and does not delete the vector. In other words, we can say that the clear() method is used to only empty an existing vector. Syntax: Vector.clear() Parameters: The method does not take any parame
2 min read
Vector addAll() Method in Java
java.util.Vector.addAll(Collection C): This method is used to append all of the elements from the collection passed as a parameter to this function to the end of a vector keeping in mind the order of return by the collection's iterator. Syntax: boolean addAll(Collection C) Parameters: The method accepts a mandatory parameter C which is a collection
2 min read
Vector set() Method in Java
The Java.util.Vector.set() method is used to replace any particular element in the vector, created using the Vector class, with another element. Syntax: Vector.set(int index, Object element) Parameters: This function accepts two mandatory parameters as shown in the above syntax and described below. index: This is of integer type and refers to the p
2 min read
Vector isEmpty() Method in Java
The Java.util.Vector.isEmpty() method in Java is used to check and verify if a Vector is empty or not. It returns True if the Vector is empty else it returns False. Syntax: Vector.isEmpty() Parameters: This method does not take any parameter. Return Value: This function returns True if the Vectoris empty else it returns False. Below programs illust
2 min read
Vector lastIndexOf() Method in Java
The Java.util.Vector.lastIndexOf(Object element) method is used to check and find the occurrence of a particular element in the vector. If the element is present in the vector then the lastIndexOf() method returns the index of last occurrence of the element otherwise it returns -1. This method is used to find the last occurrence of a particular ele
2 min read
Vector addElement() Method in Java
The Java.util.Vector.addElement() method is used to append a specified element to the end of this vector by increasing the size of the vector by 1. The functionality of this method is similar to that of the add() method of Vector class.Syntax: boolean addElement(Object element) Parameters: This function accepts a single parameter element of object
2 min read
Vector subList() Method in Java
The java.util.Vector.subList() is used to return a sublist of the existing Vector within a range mentioned in the parameter. The method takes in an upper limit and a lower limit and returns all the elements mentioned in the range. The lower limit is included if the element is present in the list and the upper limit is excluded. Basically, it takes
3 min read
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 succes
2 min read