Java Vector size( ) Method with Example

In the previous article, we have discussed about Java Vector setSize( ) Method with Example

In this article, we are going to see the use of Java Vector size() method along with examples.

Java Vector size( ) Method with Example

This java.util.Vector.sizeElement() method returns the size of the vector. This means it returns the number of elements present in the vector even the capacity of the vector is more.

Syntax:

vectorName.size( )

Where,

  • vectorName refers to the name of the Vector.

Approach:

  • Create a vector of string data type and add some elements to it.
  • Print the elements.
  • Print the vector size using size( ) method.

Program:

import java.util.*;
public class Main
{
   public static void main(String[] args) 
   {
       // Create a Vector of string datatype
       Vector<String> vec = new Vector<String>();
       // Adding some elements to the vector
       vec.add("One");
       vec.add("Two");
       vec.add("Three");
       vec.add("Four");
       vec.add("Five");
       vec.add("Six");
       vec.add("Seven");
       // Prints the vector elements
       System.out.println("The vector elements are "+vec);
       // Prints the size of the vector
       System.out.println("The size of the vector is "+vec.size());
   }
}
Output:

The vector elements are [One, Two, Three, Four, Five, Six, Seven]
The size of the vector is 7

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: