Size and capacity

ReturnMethodSummary
intcapacity()Returns the current capacity of this vector.
voidensureCapacity(int minCapacity)Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.
voidsetSize(int newSize)Sets the size of this vector.
intsize()Returns the number of components in this vector.
voidtrimToSize()Trims the capacity of this vector to be the vector's current size.

import java.util.Vector;

public class Main{

  public static void main(String args[]) {

    Vector v = new Vector(2000);

    System.out.println(v.capacity());
  }
}

The output:


2000

import java.util.Vector;

public class Main{

  public static void main(String args[]) {

    Vector v = new Vector(2000);

    System.out.println(v.capacity());
    
    v.add("java2s.com");
    
    v.trimToSize();
    
    System.out.println(v.capacity());
    
  }
}

The output:


2000
1

Set size of Vector


import java.util.Vector;

public class Main {
  public static void main(String[] args) {
    Vector v = new Vector();
    v.add("1");
    v.add("2");
    v.add("3");
    v.add("4");
    v.add("java2s.com");
    v.setSize(3);

    System.out.println(v);

    v.setSize(5);

    System.out.println(v);
  }
}

The output:


[1, 2, 3]
[1, 2, 3, null, null]
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.