Vectors and Arrays in Java

David Thompson
May 30, 2000

We know that arrays and Vectors in Java are different kinds of data structures for storing information in a random-access fashion. In Java, an object-oriented language, the two structures are implimented as classes. This presentation will demonstrate some of the principle differences between Java arrays and Java Vectors. It will also provide some examples of the cool methods that come with them.

Arrays


Arrays in Java are static lists declared to store a certain number of a certain kind of variables. It stores these values at specific, numbered locations in a list that starts at zero and goes to N-1:

myArray
Index Data
0 14
1 79
2 32
3 92
4 102

This array can be instantiated in Java with either the shorthand syntax:

int[] myArray = new int[5];
int myArray[] = new int[5];

or an explicit method call to a Java "Array" class object:

int[] myArray = Array.newInstance[int, 5];

When an array is instantiated, the array's constructor method allocates 5 integer-sized memory slots. The allocation is static- the array size cannot change after it is created. In this sense, it is analagous to a C or C++ style array. But the resemblence doesn't end here. Java Arrays, like C arrays, have undefined contents until they are initialized. One can set the contents of an array at the time of instantiation with the simple syntax:

int[] myArray = {14,34,21,42,22};

We have already seen that, in addition to the built-in C-style array operations syntax there Java provides additional classes for manipulating arrays. This represents a desire on the Java authors' part to both a) retain a grounding in C to attract those programmers already familiar with the popular language, and b) provide true object-oriented support for array operations. This is true for array referencing, as well. Various array-handling objects exist in the class library java.lang. This provides interface functions for various array operations For example, one could refer to the individual array as in C:

fourth_item = myArray[3];

or, one could use the explicit method call:

fourth_item = myArray[3];

Java arrays can also be passed as parameters to functions. Java permits either a copy of the original array or a pointer to be sent.
2D-arrays are also supported. This is a statically-allocated array of array pointers:

int[][] 2D_array = new int[x_length][y_length];

Finally, note that vectors of objects can exist. This does not mean, however, that the new objects are instantiated when the array is declared. Arrays take up a fixed size in memory, and the size of an object isn't defined when it is instantiated. Therefore, the objects must be instantiated in a seperate command. Therefore, to create an array of 5 objects, the following code could be used:

object_list = new Object[5];
object_list[0] = new Object();
object_list[1] = new Object();
.
.
.

Vectors


The key difference between Arrays and Vectors in Java is that Vectors are dynamically-allocated. They aren't declared to contain a type of variable; instead, each Vector contains a dynamic list of references to other objects. The Vector class is found in the java.util package, and extends java.util.Abstractlist.

The big advantage of using Vectors is that the size of the vector can change as needed. Vectors handle these changes through the "capacity" and "capacityIncrement" fields. When a Vector is instantiated, it declares an object array of size initialCapacity. Whenever this array fills, the vector increases the total buffer size by the value capacityIncrement. Thus, a Vector represents a compromise. It doesn't allocate each object dynamically, which would make access of middle list-items exceedingly slow; but it does allow for growth in the size of the list. The default constructor, Vector(), creates an empty Vector of initial capacity zero that doubles in size whenever it fills.

Vectors can be filled using its method addElement(Object obj). This method appends the given object to the end of the Vector. Because the Vector stores pointers to the objects, and not the objects themselves, these Vector items could be any kind of object. Moreover, several different kinds of objects could exist in the same Vector. Vector methods allow all sorts of nifty ways to manipulate data. For example, one can reference a list item not just by its index, but also by name. Elements can be removed from or added to the middle of the list:

myVector.insertElementAt(my_object, 5);
success_flag = myVector.remove(my_object);

So you're thinking, "GREAT! But if vectors are so wonderful, why even bother with arrays?"

Comparative Advantages of Arrays and Vectors


Because Vectors are dynamically-allocated, they offer a relatively memory-efficient way of handling lists whose size could change drastically. The line buffer of a text editor, for example, could be anywhere from zero to thousands of entries in size. A Vector might be a good choice for such an arrangement. Moreover, Vectors have some useful member functions that make coding simpler (see the insertElementAt and remove methods above). But because the Vector class is based on an array of object references, these methods are generally no more efficient than array-based algorithms. The insert method must perform multiple "swap" operations just as an array 'insert' algorithm would. Thus, Vectors are easier to use than arrays for most applications, but they do not offer all the performance advantages of fully-dynamic storage.




This page written by David Thompson, a student at The Department of Mathematics and Computer Science, Carleton College.