java.util.Vector.size() Method

Advertisements


Description

The size() method is used to return the number of components in this vector.

Declaration

Following is the declaration for java.util.Vector.size() method

public int size()

Parameters

  • NA

Return Value

The method call returns the number of components in this vector.

Exception

  • NA

Example

The following example shows the usage of java.util.Vector.size() method.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      // create an empty Vector vec with an initial capacity of 4      
      Vector vec = new Vector(4);

      // use add() method to add elements in the vector
      vec.add(4);
      vec.add(3);
      vec.add(2);
      vec.add(1);

      // let us print the size of the vector
      System.out.println("Size of the vector: "+vec.size());                 
   }    
}

Let us compile and run the above program, this will produce the following result.

Size of the vector: 4



Advertisements
Advertisements