Home »
Java programming language
Java Vector size() Method with Example
Vector Class size() method: Here, we are going to learn about the size() method of Vector Class with its syntax and example.
Submitted by Preeti Jain, on March 20, 2020
Vector Class size() method
- size() method is available in java.util package.
- size() method is used to return the size (i.e. the number of the element exists) of this Vector.
- size() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- size() method does not throw an exception at the time of returning size.
Syntax:
public int size();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of the method is int, it returns the size of this vector.
Example:
// Java program to demonstrate the example
// of int size() method of Vector
import java.util.*;
public class SizeOfVector {
public static void main(String[] args) {
// Instantiates a Vector object with
// initial capacity of "10"
Vector < String > v = new Vector < String > (10);
// By using add() method is to add the
// elements in this v
v.add("C");
v.add("C++");
v.add("JAVA");
// By using size() methos is to return the
// size i.e. the number of element exists
// Display Vector Size
System.out.println("v.size(): " + v.size());
}
}
Output
v.size(): 3