How to Remove All Vector Elements in Java


This example will use clear and removeAllElements method to remove all elements from Vector object in Java.

Source Code

package com.beginner.examples;

import java.util.Vector;

public class RemoveAllElementsOfVector {

	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 all elements , Vector contains : ");
		for(int index=0; index < vector.size(); index++) {
			System.out.println(vector.get(index));
		}
		
		/*
		 * To remove all elements from the Vector use  clear method.
		 */
		vector.clear();
		
		System.out.println("After removing all elements , Vector contains : ");
		for(int index=0; index < vector.size(); index++) {
			System.out.println(vector.get(index));
		}
		

	}

}

Output:

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

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments