0% found this document useful (0 votes)
30 views

Program - Demo of Vector Operation

This document demonstrates operations on a Vector in Java, including initializing a Vector with a size and increment, adding elements, checking capacity, retrieving first/last elements, checking if an element exists, enumerating elements, getting the index of an element, retrieving an element by index, inserting an element, and enumerating elements again. It creates a Vector, adds Integers and Doubles, prints out details at each step, and inserts a new element to show modification.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Program - Demo of Vector Operation

This document demonstrates operations on a Vector in Java, including initializing a Vector with a size and increment, adding elements, checking capacity, retrieving first/last elements, checking if an element exists, enumerating elements, getting the index of an element, retrieving an element by index, inserting an element, and enumerating elements again. It creates a Vector, adds Integers and Doubles, prints out details at each step, and inserts a new element to show modification.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

/* Program to demonstrates various Vector Operation */

import java.util.*;
class VectorDemo
{
public static void main(String args[ ])
{
// initial size is 4, increment is 2
Vector v = new Vector(4,2);

System.out.println("Initial size : "+v.size());


System.out.println("Initial Capacity : "+v.capacity());

v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
v.addElement(new Integer(5));

System.out.println("Capacity after five additions : "+v.capacity());


v.addElement(new Double(5.25));
System.out.println("Current Capacity : "+v.capacity());
v.addElement(new Double(10.28));
v.addElement(new Integer(8));

System.out.println("Current Capacity : "+v.capacity());


v.addElement(new Float(20.25));
v.addElement(new Integer(10));

System.out.println("Current Capacity : "+v.capacity());


v.addElement(new Integer(11));
v.addElement(new Integer(12));

System.out.println("\nFirst element :"+(Integer)v.firstElement());


System.out.println("Last element :"+(Integer)v.lastElement());

if(v.contains(new Double(5.20)))
System.out.println("Vector contains 5.20");

//****** Enumerate the elements in the Vector *********


Enumeration vEnum = v.elements();

System.out.println("\n Elements in vector :");


while(vEnum.hasMoreElements())
System.out.print(vEnum.nextElement() +" ");
System.out.println();
System.out.println("index of: 11 "+v.indexOf(11));
System.out.println("element at 5:"+v.elementAt(5));

v.insertElementAt(new Integer(100), 5);

Enumeration vEnum1 = v.elements();


System.out.println("\n Elements in vector :");
while(vEnum1.hasMoreElements())
System.out.print(vEnum1.nextElement() +" ");
System.out.println();

}
}

/*Output:
Initial size : 0
Initial Capacity : 4
Capacity after five additions : 6
Current Capacity : 6
Current Capacity : 8
Current Capacity : 10

First element :1
Last element :12

Elements in vector :
1 2 3 4 5 5.25 10.28 8 20.25 10 11 12
index of: 11 10
element at 5:5.25

Elements in vector :
1 2 3 4 5 100 5.25 10.28 8 20.25 10 11 12*/

You might also like