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.5 Generic Data Structures - Pg. 106

106 Chapter 2: Array-Based Structures elements to be copied. Since we want to copy the entire contents of the array index zero into the array data starting at index zero, the invocation would be: System.arraycopy(temp, 0, data, 0, temp.length); temp starting at The use of the method arraycopy is preferred because it is about 50% faster than the loop technique. On an absolute speed basis, tests performed on a PC using an AMD Athlon XP 3000+ processor with a 2.17-gigahertz clock showed that the arraycopy method was able to copy a 500,000 element array of object references in 0.06 seconds. What this means is that an Insert operation performed at a time when an array-based structure is full would require an extra 0.06 seconds to expand the structure's 500,000 element array. This level of performance would be adequate for most applications. Still, there are many real-time applications that could not tolerate a 0.06-second delay. One other potential downside is that expanding an array can result in a somewhat unpredictable "java.lang.OutOfMemoryError: Java heap space" run-time error. All things considered, for small- to moderately-sized array-based structures, an insert method that can expand the size of the structure's array greatly enhances the usefulness of these structures. A good middle ground implementation would add a Boolean parameter to the structure's con- structor to allow the client to specify whether or not the expandable feature of the insert method should be employed for a particular application. This implementation is left as an exercise for the student. 2.5 Generic Data Structures The development of software is an expensive and time-consuming process. Therefore, whenever possible, we should write reuseable software. In the context of data structures, reusability is achieved by writing data structures that are application independent, or generic. Generic data structures, by definition, can store any kind of node and, therefore, can be used in any application. (Naturally, the performance of the structure would have to be consistent with the requirements of the application.) Stated another way, if the final implementation of our Unsorted-Optimized array structure (the class UOAUtilities ) was generic, it could not only be used to store the data for a telephone listing application, but also an employee record application, a store inventory application, or any other type of data intense application without modifying its code. Generic implementations of data structures fall into two groupings: homogeneous and heteroge- neous (nonhomogeneous) generic data structures. Both groups can store data sets comprised of any type of node (e.g., telephone listings or employee records). However, in a homogeneous generic data structure, all nodes stored in a particular data structure object must be of the same type (e.g., all nodes in the data set are telephone listings or they are all employee records). In a het- erogeneous structure, the nodes in a single data structure object need not be all of the same type (e.g., employee records and telephone listings can be stored in the same object). The difference between homogeneous and heterogeneous structures is illustrated in Figure 2.30.