Monday, 5 May 2014

public Vector ( int initialCapacity)

public Vector(int initialCapacity)
Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero.

import java.util.*;
class VectorConstructor2{
 public static void main(String args[]){
  Vector<Integer> myVector = new Vector<> (20);
  
  System.out.print("Size of myVector is ");
  System.out.println(myVector.size());
  System.out.print("Capacity of myVector is ");
  System.out.println(myVector.capacity());
  
  /*Adding Elements to Vector */
  System.out.println("\nAdd 5 Elements to myVector");
  myVector.add(10);
  myVector.add(20);
  myVector.add(30);
  myVector.add(40);
  myVector.add(50);
  
  System.out.print("\nSize of myVector is ");
  System.out.println(myVector.size());
  System.out.print("Capacity of myVector is ");
  System.out.println(myVector.capacity());
 }
}

Output
Size of myVector is 0
Capacity of myVector is 20

Add 5 Elements to myVector

Size of myVector is 5
Capacity of myVector is 20

1. throws IllegalArgumentException if the specified initial capacity is negative
import java.util.*;
class VectorConstructor2Illegal{
 public static void main(String args[]){
  Vector<Integer> myVector = new Vector<> (-1);
 }
}

Output
Exception in thread "main" java.lang.IllegalArgumentException: Illegal Capacity: -1
        at java.util.Vector.<init>(Vector.java:129)
        at java.util.Vector.<init>(Vector.java:144)
        at VectorConstructor2Illegal.main(VectorConstructor2Illegal.java:4)


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment