What does the method int capacity() do in java?


The capacity() method is used to return the current capacity of a vector. Capacity is the length of the array kept in the vector.

Example

import java.util.Vector;
public class VectorDemo {
   public static void main(String[] args) {

      Vector<Integer> vec = new Vector<Integer>();
      vec.add(14);
      vec.add(13);
      vec.add(12);
      vec.add(11);
      vec.add(4);
      vec.add(3);
      vec.add(2);
      vec.add(1);
      System.out.println("Capacity of the vector is :"+vec.capacity());
   }
}

Output

Capacity of the vector is :10

Vikyath Ram
Vikyath Ram

A born rival

Updated on: 25-Feb-2020

191 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements