Java Vector removeElementAt(int index) Method Example

The remove(int index) method of java.util.Vector class is used to remove the element at the specified position in the Vector. Shifts any subsequent elements to the left (subtracts one from their indices). Similarly removeElementAt(int index) method also removes the element at the specified index. Each component in this vector with an index greater or equal to the specified index is shifted downward to have an index one smaller than the value it had previously.

Signature

public void removeElementAt(int index)

This method removes the element of the Vector at the given index and will throw “java.lang.IndexOutOfBoundsException”  if the index is out of range.

Example

The following example shows the usage of java.util.Vector.removeElementAt(index) method.

import java.util.Vector;

public class RemoveElementAtMethodVectorExample {
	public static void main(String args[])
	{
		// create an empty Vector
		Vector vector1 = new Vector();
		
		// use add() method to add elements to the Vector
		vector1.add("Element1");
		vector1.add("Element2");
		vector1.add("Element3");
		
		//Printing the elements of the Vector
		System.out.println("**Elements of the Vector before remove**");
		for(String temp:vector1)
		{
			System.out.println(temp);
		}
		
		//Removing the element present at the index 1
		vector1.removeElementAt(1);
		
		//Printing the elements of the Vector
		System.out.println("**Elements of the Vector after remove**");
		for(String temp:vector1)
		{
			System.out.println(temp);
		}
	}
}

Output

**Elements of the Vector before remove**
Element1
Element2
Element3
**Elements of the Vector after remove**
Element1
Element3

Leave a Reply

Your email address will not be published. Required fields are marked *