How to Get Size of Java Vector


In this example, we may see how to get size or number of elements currently stored in Vector. We may also see how to loop through the element of it.

Source Code

package com.beginner.examples;

import java.util.Vector;

public class GetSizeOfVector {

	public static void main(String[] args) {
		
	    //create Vector. 
	    Vector vector = new Vector();
	    
	    vector.add("A");
	    vector.add("B");
	    vector.add("C");
	    vector.add("D");
	    
	    /*
	     *  To set the size of Vector use setSize(int newSize) method.
	     */
	    System.out.println("Vector contains :");
	    for(int index=0; index < vector.size(); index++) {
	    	System.out.println(vector.get(index));
	    }

	}

}

Output:

Vector contains :
A
B
C
D

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments