The size() method of java.util.Vector class will return us the actual size of the vector(Count of number of elements present in the vector).
Signature
public int size()
This method returns number of elements in this vector.
Example
The following example shows the usage of java.util.Vector.size() method.
import java.util.Vector; public class SizeMethodVectorExample { 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"); vector1.add("Element4"); vector1.add("Element5"); //Getting the size of the vector System.out.println("Size of the vector is : \""+vector1.size()+"\""); //Printing the elements of the vector System.out.println("**Elements of the vector **"); for(String temp:vector1) { System.out.println(temp); } } }
Output
Size of the vector is : "5" **Elements of the vector ** Element1 Element2 Element3 Element4 Element5
Leave a Reply