How to Remove an Element from Specified Index Vector in Java


In this example we will show the common method to remove an element from Specified Index  of java Vector.

Source Code

 Vector
package com.beginner.examples;

import java.util.Vector;

public class RemoveElementFromVectorByIndex {

	public static void main(String[] args) {
		
		//create Vector.
		Vector vector = new Vector();
	    
		vector.add("A");
		vector.add("B");
		vector.add("C");
		vector.add("D");
		
		System.out.println("Before removing C , Vector contains : ");
		for(int index=0; index < vector.size(); index++) {
			System.out.println(vector.get(index));
		}
		
		/*
		 * To remove an element from the specified index of Vector use remove method.
		 */
		vector.remove(2);
		
		System.out.println("After removing C , Vector contains : ");
		for(int index=0; index < vector.size(); index++) {
			System.out.println(vector.get(index));
		}

	}

}

Output:

Before removing C , Vector contains : 
A
B
C
D
After removing C , Vector contains : 
A
B
D

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments