Remove Vector elements

We can use remove() method of Vector to delete/remove elements from Vector

There are 7 different “remove” methods defined in Vector


1) Object remove(int index);
2) boolean remove(Object o);
3) boolean removeAll(Collection c);
4) boolean removeAllElements();
5) boolean removeElement(Object o);
6) void removeElementAt(int index);
7) protected void removeRange(int fromIndex, int toIndex);

Object remove(int index)

Removes the element at the specified position in the Vector.

Example :

  1. import java.util.*;
  2. public class VectorRemoveAtIndex {
  3.     public static void main(String[] args) {
  4.     Vector<String> vector = new Vector<String>();
  5.     vector.add("Java");
  6.         vector.add("c++");
  7.         vector.add("python");
  8.         vector.add("c");
  9.         System.out.println("Vector before removing elements");
  10.         System.out.println(vector);
  11.         Vector.remove(2);
  12.         System.out.println("Vector after removing elements");
  13.         System.out.println(vector);
  14.     }
  15. }
import java.util.*;
public class VectorRemoveAtIndex {
	public static void main(String[] args) {
	Vector<String> vector = new Vector<String>();
	vector.add("Java");
        vector.add("c++");
        vector.add("python");
        vector.add("c");
        System.out.println("Vector before removing elements");
        System.out.println(vector);
        Vector.remove(2);
        System.out.println("Vector after removing elements");
        System.out.println(vector);
	}
}
Note : We can see that element at index 2 in the Vector is removed

boolean remove(Object o)

Removes the first occurrence of the specified element from the Vector, if it is present.

Example :

  1. import java.util.*;
  2. public class VectorRemoveElement {
  3.     public static void main(String[] args) {
  4.     Vector<String> vector = new Vector<String>();
  5.     vector.add("java");
  6.         vector.add("c++");
  7.         vector.add("python");
  8.         vector.add("java");
  9.         System.out.println("Vector before removing elements");
  10.         System.out.println(vector);
  11.         Vector.remove("java");
  12.         System.out.println("Vector after removing elements");
  13.         System.out.println(vector);
  14.     }
  15. }
import java.util.*;
public class VectorRemoveElement {
	public static void main(String[] args) {
	Vector<String> vector = new Vector<String>();
	vector.add("java");
        vector.add("c++");
        vector.add("python");
        vector.add("java");
        System.out.println("Vector before removing elements");
        System.out.println(vector);
        Vector.remove("java");
        System.out.println("Vector after removing elements");
        System.out.println(vector);
	}
}
Note : We can see that , first occurance of element “java” is removed from the Vector

boolean removeAll(Collection c)

Removes from the Vector all of its elements that are contained in the specified collection.

Example :

  1. import java.util.*
  2. public class VectorRemoveAllElements {
  3.     public static void main(String[] args) {
  4.     Vector<String> vector1 = new Vector<String>();
  5.     vector1.add("java");
  6.         vector1.add("c++");
  7.         vector1.add("python");
  8.         vector1.add("c");
  9.         Vector<String> vector2 = new Vector<String>();
  10.         vector2.add("c++");
  11.         vector2.add("python");
  12.         System.out.println("Vector before removing elements");
  13.         System.out.println(vector2);
  14.         Vector1.removeAll(vector2);
  15.         System.out.println("Vector after removing elements");
  16.         System.out.println(vector1);
  17.     }
  18. }
import java.util.*
public class VectorRemoveAllElements {
	public static void main(String[] args) {
	Vector<String> vector1 = new Vector<String>();
	vector1.add("java");
        vector1.add("c++");
        vector1.add("python");
        vector1.add("c");
        Vector<String> vector2 = new Vector<String>();
        vector2.add("c++");
        vector2.add("python");
        System.out.println("Vector before removing elements");
        System.out.println(vector2);
        Vector1.removeAll(vector2);
        System.out.println("Vector after removing elements");
        System.out.println(vector1);
	}
}
Note : We can see that “removeAll()” method has removed all of its elements “c++” and “python” from the main 
            Vector

boolean removeAllElements()

Removes all elements from this vector and sets its size to zero

Example :

  1. import java.util.*;
  2. public class VectorRemoveAllElements {
  3.     public static void main(String[] args) {
  4.     Vector<String> vector = new Vector<String>();
  5.     vector.add("java");
  6.         vector.add("c++");
  7.         vector.add("python");
  8.         vector.add("java");
  9.         System.out.println("Vector before removing elements");
  10.         System.out.println(vector);
  11.         vector.removeAllElements();
  12.         System.out.println("Vector after removing elements");
  13.         System.out.println(vector);
  14.     }
  15. }
import java.util.*;
public class VectorRemoveAllElements {
	public static void main(String[] args) {
	Vector<String> vector = new Vector<String>();
	vector.add("java");
        vector.add("c++");
        vector.add("python");
        vector.add("java");
        System.out.println("Vector before removing elements");
        System.out.println(vector);
        vector.removeAllElements();
        System.out.println("Vector after removing elements");
        System.out.println(vector);
	}
}

boolean removeElement(Object o)

Removes the first (lowest-indexed) occurrence of the argument from this vector.

Example :

  1. import java.util.*;
  2. public class VectorRemoveElement {
  3.     public static void main(String[] args) {
  4.     Vector<String> vector = new Vector<String>();
  5.     vector.add("java");
  6.         vector.add("c++");
  7.         vector.add("python");
  8.         vector.add("java");
  9.         System.out.println("Vector before removing elements");
  10.         System.out.println(vector);
  11.         vector.removeElement("python");
  12.         System.out.println("Vector after removing elements");
  13.         System.out.println(vector);
  14.     }
  15. }
import java.util.*;
public class VectorRemoveElement {
	public static void main(String[] args) {
	Vector<String> vector = new Vector<String>();
	vector.add("java");
        vector.add("c++");
        vector.add("python");
        vector.add("java");
        System.out.println("Vector before removing elements");
        System.out.println(vector);
        vector.removeElement("python");
        System.out.println("Vector after removing elements");
        System.out.println(vector);
	}
}

void removeElementAt(int index)

Deletes the element at the specified index.

Example :

  1. import java.util.*;
  2. public class VectorRemoveElementAtIndex {
  3.     public static void main(String[] args) {
  4.     Vector<String> vector = new Vector<String>();
  5.     vector.add("java");
  6. vector.add("c++");
  7. vector.add("python");
  8. vector.add("java");
  9. System.out.println("Vector before removing elements");
  10. System.out.println(vector);
  11. vector.removeElementAt(2);
  12. System.out.println("Vector after removing elements");
  13. System.out.println(vector);
  14.     }
  15. }
import java.util.*;
public class VectorRemoveElementAtIndex {
	public static void main(String[] args) {
	Vector<String> vector = new Vector<String>();
	vector.add("java");
vector.add("c++");
vector.add("python");
vector.add("java");
System.out.println("Vector before removing elements");
System.out.println(vector);
vector.removeElementAt(2);
System.out.println("Vector after removing elements");
System.out.println(vector);
	}
}

protected void removeRange(int fromIndex, int toIndex)

Removes from this Vector all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.

Note : removeRange(int, int) method is protected . 
           
           We can only invoke it from a subclass of Vector or from a class within the same package as Vector

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on