Like an Array, but More Dynamic

Problem

You don’t want to worry about storage reallocation; you want a standard class to handle it for you.

Solution

Use a Vector. Or, in Java 2, an ArrayList.

Discussion

A Vector is just a standard class that encapsulates the functionality of an array but allows it to expand automatically. You can just keep on adding things to it, and each addition will behave the same. If you watch really closely you might notice a brief extra pause once in a while when adding objects, as Vector reallocates and copies. But you don’t have to think about it.

However, because Vector is a class and isn’t part of the syntax of Java, you can’t use Java’s array syntax; you must use methods to access the Vector data. There are methods to add objects, retrieve objects, find objects, and tell you how big the Vector is and how big it can become without having to reallocate. Like those of all the collection classes in java.util, Vector’s storing and retrieval methods are defined in terms of java.lang.Object. But since Object is the ancestor of every defined type, you can store objects of any type in a Vector (or any collection), and cast it when retrieving it. If you need to store a small number of built-ins (like int, float, etc.) into a collection containing other data, use the appropriate wrapper class (see the Introduction to Chapter 5). To store booleans, either use a java.util.BitSet (see the online documentation) or the Boolean wrapper class.

Table 7-1 shows some of the most important methods of Vector. Equally important, those listed are also methods of the List interface, which we’ll discuss shortly. This means that the same methods can be used with the newer ArrayList class and several other classes.

Table 7-1. List access methods

Method signature

Usage

add(Object o)

Add the given element at the end

add(int i, Object o)

Insert the given element at the specified position

clear( )

Remove all element references from the Collection

contains(Object o)

True if the Vector contains the given Object

get(int i)

Return the object reference at the specified position

indexOf(Object o)

Return the index where the given object is found, or -1

remove(Object o)

remove(int i)

Remove an object by reference or by position

toArray( )

Return an array containing the objects in the Collection

This program, VectorDemo, stores data in a Vector and retrieves it for processing:

Vector     v = new Vector(  );
    StructureDemo source = new StructureDemo(15);

    // Add lots of elements to the Vector...
    v.add(source.getDate(  ));
    v.add(source.getDate(  ));
    v.add(source.getDate(  ));

    // Process the data structure using a for loop.
    System.out.println("Retrieving by index:");
    for (int i = 0; i<v.size(  ); i++) {
        System.out.println("Element " + i + " = " + v.get(i));
    }

Note that Vector and Hashtable pre-date the Collections framework, so they provide methods with different names: Vector provides addElement( ) and elementAt( ). In new code you should generally use the Collections methods add( ) and get( ). The equivalent program done using an ArrayList (ArrayListDemo.java) looks like this:

ArrayList al = new ArrayList(  );

    // Create a source of Objects
    StructureDemo source = new StructureDemo(15);

    // Add lots of elements to the ArrayList...
    al.add(source.getDate(  ));
    al.add(source.getDate(  ));
    al.add(source.getDate(  ));

    // First print them out using a for loop.
    System.out.println("Retrieving by index:");
    for (int i = 0; i<al.size(  ); i++) {
        System.out.println("Element " + i + " = " + al.get(i));
    }

As you can see, the structure is very similar. You might wonder, then, why they added ArrayList and didn’t just keep Vector. One major difference is that the methods of Vector are synchronized, meaning that they can be accessed from multiple threads (see Section 24.6). This does mean more overhead, though, so in a single-threaded application it may be faster to use an ArrayList (see timing results in Section 7.19).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.194