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 :
- 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);
- }
- }
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 :
- 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);
- }
- }
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 :
- 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);
- }
- }
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 :
- 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);
- }
- }
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 :
- 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);
- }
- }
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 :
- 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);
- }
- }
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