The Wayback Machine - https://web.archive.org/web/20240204231511/https://www.geeksforgeeks.org/vector-removeelement-method-in-java-with-example/
Open In App
Related Articles

Vector removeElement() method in Java with Example

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The java.util.Vector.removeElement() method is used to remove first occurrence of particular object. If object is not found then it returns false else it returns true. If a particular object is present inside vector and removeElement() method call on that vector element then this method reduces vector size by 1. 

Syntax:

public boolean removeElement(Object obj)

Parameters: This function accepts object as parameter which is to be removed. 

Return Type: On Successful of deletion this function returns True otherwise this function returns False. 

Exceptions: This method does not raise any exception. 

Below programs illustrates the Vector.removeElement() function. 

Program 1: 

Java




// Java program to understand
// about vector.removeElement() function
 
// because vector is present in this package
import java.util.*;
 
// Driver Code
public class vector_demo {
    // main method begins here
    public static void main(String[] args)
    {
        // creating vector type object
        Vector<Integer> v
            = new Vector<Integer>();
 
        // inserting elements into the vector
        v.add(1);
        v.add(2);
        v.add(3);
        v.add(4);
        v.add(5);
        v.add(6);
 
        // printing vector before deleting element
        System.out.println("Before deleting");
        System.out.println("Vector: " + v);
        System.out.println("Size: " + v.size());
 
        System.out.println("\nAfter deleting");
 
        // trying to deleting object 3
        boolean flag = v.removeElement(3);
        if (flag) {
            System.out.println("Element '3' has been removed");
        }
        else {
            System.out.println("Element '3' is not present in Vector");
        }
 
        System.out.println("Vector: " + v);
        System.out.println("Size: " + v.size());
    }
}


Output:

Before deleting
Vector: [1, 2, 3, 4, 5, 6]
Size: 6

After deleting
Element '3' has been removed
Vector: [1, 2, 4, 5, 6]
Size: 5

Example 2: 

Java




// Java program to understand
// about vector.removeElement() function
 
// because vector is present in this package
import java.util.*;
 
// Driver Code
public class vector_demo {
    // main method begins here
    public static void main(String[] args)
    {
        // creating vector type object
        Vector<Integer> v = new Vector<Integer>();
        // inserting elements into the vector
        v.add(1);
        v.add(2);
        v.add(3);
        v.add(4);
        v.add(5);
        v.add(6);
 
        // printing vector before deleting element
        System.out.println("Before deleting");
        System.out.println("Vector: " + v);
        System.out.println("Size: " + v.size());
 
        System.out.println("\nAfter deleting");
 
        // trying to deleting object 15
        boolean flag = v.removeElement(15);
 
        // since object 15 is not present flag will be false
        if (flag) {
            System.out.println("Element '15' has been removed");
        }
        else {
            System.out.println("Element '15' is not present in Vector");
        }
 
        System.out.println("Vector: " + v);
        System.out.println("Size: " + v.size());
    }
}


Output:

Before deleting
Vector: [1, 2, 3, 4, 5, 6]
Size: 6

After deleting
Element '15' is not present in Vector
Vector: [1, 2, 3, 4, 5, 6]
Size: 6

Time complexity: O(n). // n is the size of the vector.
Space complexity: O(1).


Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:

  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 24 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials