Java Data Structures - Contents
A D V E R T I S E M E N T
The java.util.Vector class is provided
by the Java API, and is one of the most useful array based data storage classes I've ever
seen. The information provided here is as far as JDK 1.2 goes, future versions may have
other implementations; still, the functionality should remain the same. A vector, is a
growing array; as more and more elements are added onto it, the array grows. There is also
a possibility of making the array smaller.
But wait a minute, all this time I've been saying
that arrays can't grow or shrink, and it seems Java API has done it. Not quite. The java.util.Vector
class doesn't exactly grow, or shrink. When it needs to do these operations, it simply
allocates a new array (of appropriate size), and copies the contents of the old array into
the new array. Thus, giving the impression that the array has changed size.
All these memory operations can get quite expensive
if a Vector is used in a wrong way. Since a Vector has a similar
architecture to the array stack we've designed earlier, the best and fastest way to
implement a Vector is to do stack operations. Usually, in programs, we need a
general data storage class, and don't really care about the order in which things are
stored or retrieved; that's where java.util.Vector comes in very useful.
Using a Vector to simulate a queue is
very expensive, since every time you insert or remove, the entire array has to be copied
(not necessarily reallocated but still involves lots of useless work).
Vector allows us to view it's insides
using an Enumerator ; a class to go through objects. It is very useful to
first be able to look what you're looking for, and only later decide whether you'd like to
remove it or not. A sample program that uses java.util.Vector for it's
storage follows.
import java.io.*;
import java.util.*;
class pVectorTest{
public static void main(String[] args){
Vector v = new Vector(15);
Integer j = null;
int i;
System.out.println("starting...");
for(i=0;i<10;i++){
j = new Integer((int)(Math.random() * 100));
v.addElement(j);
System.out.println("addElement: " + j);
}
System.out.println("size: "+v.size());
System.out.println("capacity: "+v.capacity());
Enumeration enum = v.elements();
while(enum.hasMoreElements())
System.out.println("enum: "+(Integer)enum.nextElement());
System.out.println("Done ;-)");
}
}
The example above should be self evident (if you
paid attention when I showed test programs for the previous data structures). The main key
difference is that this one doesn't actually remove objects at the end; we just leave them
inside. Removal can be accomplished very easily, and if you'll be doing anything cool with
the class, you'll sure to look up the API specs.
Printing is accomplished using an Enumerator ;
which we use to march through every element printing as we move along. We could also have
done the same by doing a for loop, going from 0 to v.size() ,
doing a v.elementAt(int) every time through the loop. The output from the
above program follows:
starting...
addElement: 9
addElement: 5
addElement: 54
addElement: 49
addElement: 60
addElement: 81
addElement: 8
addElement: 91
addElement: 76
addElement: 81
size: 10
capacity: 15
enum: 9
enum: 5
enum: 54
enum: 49
enum: 60
enum: 81
enum: 8
enum: 91
enum: 76
enum: 81
Done ;-)
You should notice that when we print the size
and capacity , they're different. The size is the current number
of elements inside the Vector , and the capacity , is the maximum
possible without reallocation.
A trick you can try yourself when playing with the Vector
is to have Vectors of Vectors (since Vector is also
an Object , there shouldn't be any problems of doing it). Constructs like that
can lead to some interesting data structures, and even more confusion. Just try inserting
a Vector into a Vector ;-)
I guess that covers the Vector class.
If you need to know more about it, you're welcome to read the API specs for it. I also
greatly encourage you to look at java.util.Vector source, and see for
yourself what's going on inside that incredibly simple structure.
Back to Table of Contents
A D V E R T I S E M E N T
|
Subscribe to SourceCodesWorld - Techies Talk |
|