0% found this document useful (0 votes)
7 views4 pages

Java Vector Class Overview

Java's Vector class in the java.util package allows the creation of a dynamic array that can hold objects of any type and size. It has constructors for setting initial capacity and increment for resizing, and it automatically allocates extra space when the capacity is exceeded. The document also includes a sample program demonstrating various methods of the Vector class, such as adding, inserting, and setting elements.

Uploaded by

forlornkid16
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)
7 views4 pages

Java Vector Class Overview

Java's Vector class in the java.util package allows the creation of a dynamic array that can hold objects of any type and size. It has constructors for setting initial capacity and increment for resizing, and it automatically allocates extra space when the capacity is exceeded. The document also includes a sample program demonstrating various methods of the Vector class, such as adding, inserting, and setting elements.

Uploaded by

forlornkid16
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

Vector

Java provides a class called Vector contained in the [Link] package, using which we can create a
generic dynamic array known as vector that can hold objects (and only them) of any type and of any
number. It may also be noted that the objects stored in a vector don’t need to be homogenous.

Here are the Vector constructors:


Vector( )
Vector(int cap)
Vector(int cap, int incr)

The first form creates a default vector, which has an initial capacity of 10. The second form creates a
vector whose initial capacity is specified by cap. The third form creates a vector whose initial capacity
is specified by cap and whose increment is specified by incr. The increment specifies the number of
elements to allocate for each time a vector is resized upward.

All vectors start with an initial capacity. After this initial capacity is reached, the next time that you
attempt to store an object in the vector, the vector automatically allocates space for that object plus extra
room for additional objects. By allocating more than just the required memory, the vector reduces the
number of allocations that must take place. This reduction is important, because allocations are costly
in terms of time. The amount of extra space allocated during each reallocation is determined by the
increment that you specify when you create the vector. If you don’t specify an increment, the vector’s
size is doubled by each allocation cycle.

Few important methods defined by the Vector class:


Note:
insertElementAt() works by first upward shifting all the elements starting from the one
at the index specified, and then storing the object supplied at that index, whereas
setElementAt() works by simply overwriting at the index specified with the argument
object.
A sample programme on Vector:
import [Link].*;
import [Link].*;

class Test
{
int x;
Test( int a)
{ x=a; }
}

class VectorTest
{
public static void main(String args[]) throws ArrayIndexOutOfBoundsException
{
Vector list=new Vector(3,2);
[Link]("Initial capacity: "+[Link]());
[Link]("Initial size: "+[Link]());
int l=[Link];
int i;

for(i=0; i<l; i++)


[Link](args[i]);

[Link]("Current capacity: "+[Link]());


[Link]("Current size: "+[Link]());

try{
[Link]("Guest-1", 2);
[Link]("Guest-2", 3);

[Link]("Current capacity: "+[Link]());


[Link]("Current size: "+[Link]());

int s1=[Link]();
String arr1[] = new String[s1];
[Link](arr1);

for(i=0; i<s1; i++)


[Link](arr1[i]);

[Link]("Guest-3", 4);

[Link]("Current capacity: "+[Link]());


[Link]("Current size: "+[Link]());

int s2=[Link]();
String arr2[] = new String[s2];
[Link](arr2);

for(i=0; i<s2; i++)


[Link](arr2[i]);

[Link](new Integer(5)); // Using wrapper class Integer


[Link](new Float(7.5f)); // Using wrapper class Float
[Link]("Current capacity: "+[Link]());
[Link]("Current size: "+[Link]());

[Link](new Test(5));
[Link](new Test(10), 4);
[Link](new Test(20), 6);

[Link]("Current capacity: "+[Link]());


[Link]("Current size: "+[Link]());

[Link](((Test)[Link](4)).x);
[Link](((Test)[Link](6)).x);
}
catch(ArrayIndexOutOfBoundsException aioobe)
{
[Link]("Array index is out of range!");
}
[Link]("Show me before exit");
}
}

Output:.

You might also like