010605
J. W. Rider

Data in the Aggregate

Arrays

An array characterizes a number of variables of the same type that are identified by an integer index and name rather than by a name alone.

   MyClass[] myArray;
creates a reference to an array whose elements are of type "MyClass"
   myArray = new MyClass[10];
creates an array of ten elements (index 0 to 9).
   for (int i=myArray.length-1; i>=0; i--)
        myArray[i] = new MyClass();
create ten instances of MyClass and store them in array
   int i=0;
   for ( ; i< myArray.length; i++)
        if (tested(myArray[i]))
           break;
find first occurrence of tested element in array


Instances of arrays are objects, just as instances of classes are. The elements of arrays may be either primitive and reference variables. The number of elements in an array is fixed when the array object is created. The number of elements in an array may be accessed through the array's length field.

Vectors

The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.

   // import java.util.Vector;

  Vector myVector = new Vector();
create a new vector object
  for (int i=1; i<= 10; i++)
      myVector.add(new MyClass());
add ten instances of MyClass to the vector
  int i=0;
  for (  ; i< myVector.size(); i++)
      if (tested(myVector.elementAt(i))
         break;
find first occurrence of tested element in vector.


The elements of a vector must be objects; you cannot store primitive values into vectors. However, this is not much of a restriction. The primitive types have wrapper classes defined in java.lang to bypass this restriction. 1