Recipe 7.3 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 an ArrayList.

Discussion

ArrayList is 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 behaves the same. If you watch really closely, you might notice a brief extra pause once in a while when adding objects as the ArrayList reallocates and copies. But you don't have to think about it.

However, because ArrayList 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 ArrayList's data. It has methods to add objects, retrieve objects, find objects, and tell you how big the List is and how big it can become without having to reallocate (note that the ArrayList class is but one implementation of the List interface; more on that later). Like the collection classes in java.util, ArrayList'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 List (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 boolean s, either store them directly in a java.util.BitSet (see the online documentation) or store them in a List using the Boolean wrapper class.

Table 7-1 shows some of the most important methods of ArrayList. 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 older Vector 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 List 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


ArrayListDemo stores data in an ArrayList and retrieves it for processing:

List 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)); }

The older Vector and Hashtable classes predate the Collections framework, so they provide additional methods with different names: Vector provides addElement( ) and elementAt( ). In new code, you should use the Collections methods add( ) and get( ) instead. Another difference is that the methods of Vector are synchronized, meaning that they can be accessed from multiple threads (see Recipe 24.5). This does mean more overhead, though, so in a single-threaded application it may be faster to use an ArrayList (see timing results in Recipe 7.17).



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net