Tuesday, 9 December 2014

Java : Collection Framework : Vector (Capacity)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=3MaZyBXc-_w&list=UUhwKlOVR041tngjerWxVccw

VectorExample.java
import java.util.Vector;

/*
 *  Example of capacity() method 
 */
public class VectorExample
{

    public static void main(String[] args)
    {
        Vector<Integer> vector = new Vector<Integer>();

        System.out.println("vector  : " + vector);
        System.out.println("vector size : " + vector.size());

        /*
         * Returns the current capacity of this vector.
         */
        int capacity = vector.capacity();

        System.out.println("capacity  : " + capacity + "\n");

        for (int i = 0; i < 11; i++)
        {
            vector.add(i+1);
        }

        System.out.println("vector  : " + vector);
        System.out.println("vector size : " + vector.size());

        capacity = vector.capacity();
        System.out.println("capacity  : " + capacity + "\n");
        
        int j = vector.size()+1;
        for (int i = 0; i < 11; i++)
        {
            vector.add(i+j);
        }

        System.out.println("vector  : " + vector);
        System.out.println("vector size : " + vector.size());

        capacity = vector.capacity();
        System.out.println("capacity  : " + capacity + "\n");

    }
}

Output
vector  : []
vector size : 0
capacity  : 10

vector  : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
vector size : 11
capacity  : 20

vector  : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
vector size : 22
capacity  : 40

To Download VectorDemoCapacity Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/VectorDemoCapacity.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment