Thursday, February 23, 2012

Vector class example in JAVA | Explanatory code

The Vector class implements a growable array of objects.The size of a Vector can grow or shrink.
You can insert or delete element from the Vector.
Following is a self explanatory program to explain working of Vectors in JAVA


import java.util.*;
class VectorTest{
public static void main(String[] args){
Vector v1 = new Vector(2,3);//size 2 capacity increment by 3 when overflow
Vector v2=new Vector();//size 10 increment (double when overflow)
Vector v3=new Vector(3);//size 3 and increment double when overflow
System.out.println("Capacities:"+v1.capacity()+" "+ v2.capacity()+" "+ v3.capacity());
System.out.println("Sizes:"+v1.size()+" "+ v2.size()+" "+ v3.size());
int i = 101;
Integer wi = new Integer(230);
String str = "Abhas";
v1.addElement(i);
v1.add(wi);//both addElement and add work
//after adding two elements
System.out.println("1.Capacities:"+v1.capacity()+" "+ v2.capacity()+" "+ v3.capacity());
System.out.println("1.Sizes:"+v1.size()+" "+ v2.size()+" "+ v3.size());
v1.add(str);
//when third element is added
System.out.println("2.Capacities:"+v1.capacity()+" "+ v2.capacity()+" "+ v3.capacity());
System.out.println("2.Sizes:"+v1.size()+" "+ v2.size()+" "+ v3.size());
v1.add(new Integer(1));
v1.add(new Integer(1));
v1.add(new Integer(1));
//after adding 3 more elements
System.out.println("3.Capacities:"+v1.capacity()+" "+ v2.capacity()+" "+ v3.capacity());
System.out.println("3.Sizes:"+v1.size()+" "+ v2.size()+" "+ v3.size());
v3.add(new Float(1.3f));v3.add(new Float(1.3f));v3.add(new Float(1.3f));
//after adding 3 elements in v3
System.out.println("4.Capacities:"+v1.capacity()+" "+ v2.capacity()+" "+ v3.capacity());
System.out.println("4.Sizes:"+v1.size()+" "+ v2.size()+" "+ v3.size());
v3.add(new Float(1.3f));
//after adding one more element in v3
System.out.println("5.Capacities:"+v1.capacity()+" "+ v2.capacity()+" "+ v3.capacity());//capacity of v3 doubles (becomes 6)
System.out.println("5.Sizes:"+v1.size()+" "+ v2.size()+" "+ v3.size());//size of v3 4
v3.add(new Float(1.3f));v3.add(new Float(1.3f));v3.add(new Float(1.3f));
//now size of v3 becomes 7
//so capaity doubles again
System.out.println("6.Capacities:"+v1.capacity()+" "+ v2.capacity()+" "+ v3.capacity());//capacity of v3 doubles (becomes 12)
System.out.println("6.Sizes:"+v1.size()+" "+ v2.size()+" "+ v3.size());//size of v3 7
v2.add(new Integer(1));v2.add(new Integer(1));v2.add(new Integer(1));v2.add(new Integer(1));v2.add(new Integer(1));
v2.add(new Integer(1));v2.add(new Integer(1));v2.add(new Integer(1));v2.add(new Integer(1));v2.add(new Integer(1));
v2.add(new Integer(1));
//after adding 11 elements in v2
System.out.println("7.Capacities:"+v1.capacity()+" "+ v2.capacity()+" "+ v3.capacity());//capacity of v3 doubles
System.out.println("7.Sizes:"+v1.size()+" "+ v2.size()+" "+ v3.size());
System.out.println("Manupulating vector v1");
System.out.println("The elements of vector: " + v1);
System.out.println("The elements at position 3 is: " + v1.elementAt(3));
System.out.println("The first element of vector is: " + v1.firstElement());
System.out.println("The last element of vector is: " + v1.lastElement());
v1.removeElementAt(2);
Enumeration e=v1.elements();
System.out.println("The elements of vector: " + v1);
while(e.hasMoreElements()){
System.out.println("The elements are: " + e.nextElement());
}
}
}

Output:
Capacities:2 10 3
Sizes:0 0 0
1.Capacities:2 10 3
1.Sizes:2 0 0
2.Capacities:5 10 3
2.Sizes:3 0 0
3.Capacities:8 10 3
3.Sizes:6 0 0
4.Capacities:8 10 3
4.Sizes:6 0 3
5.Capacities:8 10 6
5.Sizes:6 0 4
6.Capacities:8 10 12
6.Sizes:6 0 7
7.Capacities:8 20 12
7.Sizes:6 11 7
Manupulating vector v1
The elements of vector: [101, 230, Abhas, 1, 1, 1]
The elements at position 3 is: 1
The first element of vector is: 101
The last element of vector is: 1
The elements of vector: [101, 230, 1, 1, 1]
The elements are: 101
The elements are: 230
The elements are: 1
The elements are: 1
The elements are: 1

No comments:

Post a Comment