Thursday, September 1, 2011

Vector example


NOTE: Do the STEPs one by one and njoy
//VectorExample.java

import java.util.Iterator;
import java.util.Vector;

public class VectorExample
{
public static void main(String a[])
{
Vector v = new Vector();

//START STEP1
System.out.println("Inital Capacity: "+v.capacity());
System.out.println("Inital Size: "+v.size());
//END STEP1

//START STEP2
for(int i=0;i<8;i++)
{
//v.addElement(i);
v.add(i);
}

System.out.println("After adding 8 elements in vector.");
System.out.println("Now Vector Capacity: "+v.capacity());
System.out.println("Now Vector Size: "+v.size());
System.out.println("Now the Vector is as follows:");
Iterator it1 = v.iterator();
while(it1.hasNext())
{
System.out.println("Vector: "+it1.next());
}
//END STEP2

//START STEP3
//Adding more 4 elements in Vector.
v.add(8, 100);
v.add(9, 200);
v.add(10, 300);
v.add(11, 400);
System.out.println("After adding more 4 elements in Vector:");
System.out.println("Now Vector Capacity: "+v.capacity());
System.out.println("Now Vector Size: "+v.size());
System.out.println("Now the Vector is as follows:");
Iterator it2 = v.iterator();
while(it2.hasNext())
{
System.out.println("Vector: "+it2.next());
}
//END STEP3

//START STEP4
//Set the value of index 10 as 500
v.setElementAt(500, 10);
System.out.println("After setting the value of index 10 from 300 to 500 then the vector is as follows:");
Iterator it3 = v.iterator();
while(it3.hasNext())
{
System.out.println("Vector: "+it3.next());
}
//END STEP4

//START STEP5
//Set the vector size=15
System.out.println("After setting Vector size 15");
v.setSize(15);
System.out.println("Now Vector Size: "+v.size());
System.out.println("Now Vector Capacity: "+v.capacity());

System.out.println("Now the Vector is as follows:");
Iterator it4 = v.iterator();
while(it4.hasNext())
{
System.out.println("Vector: "+it4.next());
}
//END STEP5

//START STEP6
//Insert more 6 element in vector:
System.out.println("Insert more 6 element in vector:");
for(int j=0;j<6;j++)
{
v.add(j);
}

System.out.println("Now Vector Size: "+v.size());
//Vector capacity always increase by double in respective of it before capacity if its size cross its before capacity.
System.out.println("Now Vector Capacity: "+v.capacity());

System.out.println("Now the Vector is as follows:");
Iterator it5 = v.iterator();
while(it5.hasNext())
{
System.out.println("Vector: "+it5.next());
}
//END STEP6

}
}

Output =>
Inital Capacity: 10
Inital Size: 0
After adding 8 elements in vector.
Now Vector Capacity: 10
Now Vector Size: 8
Now the Vector is as follows:
Vector: 0
Vector: 1
Vector: 2
Vector: 3
Vector: 4
Vector: 5
Vector: 6
Vector: 7
After adding more 4 elements in Vector:
Now Vector Capacity: 20
Now Vector Size: 12
Now the Vector is as follows:
Vector: 0
Vector: 1
Vector: 2
Vector: 3
Vector: 4
Vector: 5
Vector: 6
Vector: 7
Vector: 100
Vector: 200
Vector: 300
Vector: 400
After setting the value of index 10 from 300 to 500 then the vector is as follows:
Vector: 0
Vector: 1
Vector: 2
Vector: 3
Vector: 4
Vector: 5
Vector: 6
Vector: 7
Vector: 100
Vector: 200
Vector: 500
Vector: 400
After setting Vector size 15
Now Vector Size: 15
Now Vector Capacity: 20
Now the Vector is as follows:
Vector: 0
Vector: 1
Vector: 2
Vector: 3
Vector: 4
Vector: 5
Vector: 6
Vector: 7
Vector: 100
Vector: 200
Vector: 500
Vector: 400
Vector: null
Vector: null
Vector: null
Insert more 6 element in vector:
Now Vector Size: 21
Now Vector Capacity: 40
Now the Vector is as follows:
Vector: 0
Vector: 1
Vector: 2
Vector: 3
Vector: 4
Vector: 5
Vector: 6
Vector: 7
Vector: 100
Vector: 200
Vector: 500
Vector: 400
Vector: null
Vector: null
Vector: null
Vector: 0
Vector: 1
Vector: 2
Vector: 3
Vector: 4
Vector: 5

No comments:

Post a Comment