Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


Share this Page URL
Help

Chapter 2. Array-Based Structures > 2.6 Java's ArrayList Class - Pg. 118

118 Chapter 2: Array-Based Structures would not result in a compile error. It should be noted that for most applications it is better pro- gramming practice to declare two homogeneous data structures that each store one type of node, rather than one heterogeneous structure that stores two different types of nodes. 2.6 Java's ArrayList Class The Java Application Programmer Interface provides a class named ArrayList that is similar to the language's array construct but in fact has much more in common with the array-based structures developed in this chapter. Like arrays, access into the structure is in the node number mode 16 via an integer index whose minimum value is zero, and the structure is unencapsulated. Unlike arrays, the structure ArrayList supports all four basic operations (Insert, Fetch, Delete, and Update), is implemented using generics, cannot store primitive types, 17 and can be used either as compiler- enforced homogeneous or heterogeneous structure. Considering these characteristics, an ArrayList is actually an unencapsulated node number mode access version of our generic array- based structure. The ArrayList structure does have one additional feature that neither arrays nor our array-based structures possess. Although we can specify an initial number of nodes to be stored in the structure, ArrayList <Node> NYC = new ArrayList<Node>(200); // initially 200 node // capacity an ArrayList object can expand at run-time beyond its initial size to accommodate unantici- pated Insert operations. As discussed, Insert operation implementations that support run-time expansion run more slowly than those that do not. Table 2.5 presents the names of the basic operation methods in the class ArrayList and some examples of their use. It assumes that the ArrayList structure boston was declared as a heteroge- neous structure with the statement: ArrayList boston = new ArrayList(); // initial size defaults to 10 nodes Node that the variables reference. tom and mary store references to objects and temp can store a Node 16 The one exception is that the Insert operation assigns the next node number to the inserted node, which makes the struc- ture a little awkward to use. 17 Java 5.0 gives the appearance of allowing primitives to be inserted into an primitive in a Wrapper object before inserting it. ArrayList object but it actually wraps the