Java Utility Library

Java Vector - size() Method



The java.util.Vector.size() method returns the number of elements in the vector.

Syntax

public int size()

Parameters

No parameter is required.

Return Value

Returns the number of elements in the vector.

Exception

NA

Example:

In the example below, the java.util.Vector.size() method is used to find out the number of elements in the vector.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector
    Vector<Integer> MyVector = new Vector<Integer>();

    //populating vector
    MyVector.add(10);
    MyVector.add(20);
    MyVector.add(30);

    System.out.println("Size of MyVector: " + MyVector.size());    

    //adding one more element in the vector
    System.out.println("40 is added in MyVector."); 

    System.out.println("Now, Size of MyVector: " + MyVector.size());  
  }
}

The output of the above code will be:

Size of MyVector: 3
40 is added in MyVector.
Now, Size of MyVector: 4

❮ Java.util - Vector