Case Study: Building A Dynamic Array


Introduction

When considering all functional assets of the Java Platform API, none possess the extraordinary potential to save you more time and hassle than the Java collections framework. Located in the java.util package, the interfaces, classes, algorithms, and infrastructure code belonging to the Java collections framework provide you with a unified way to manipulate collections of objects.

This chapter introduces you to the Java collections framework and shows you how to employ its interfaces and classes in your programs. Along the way you will learn the general characteristics of lists, sets, and maps, and the functional characteristics of linked lists, hashtables, and red-black trees. Knowledge of these concepts will set the stage for understanding the inner workings of collections framework classes.

From the beginning, the Java Platform API has undergone continuous evolutionary improvement. Nowhere is this more evident than in the Java collections framework. Each subsequent release of the Java platform introduces improved collections capability.

Continuous evolutionary improvement is a good thing for programmers. Each new release of the Java API fixes bugs discovered in the previous release. Less bugs mean more stability. That’s a good thing. On the other hand, improvements to the Java platform can present significant challenges to programmers. Each new release introduces, in some cases, sweeping new functionality. Such is the case between Java platform version 1.4.x and version 5.

The differences between the Java 5 collections framework and previous versions are significant. Java 5 introduced Java generics. It also introduced autoboxing and unboxing of primitive data types and an enhanced for loop for processing collections. These changes reduce and sometimes eliminate the need to utilize certain coding styles previously required to manipulate collections in earlier platform versions. For example, earlier versions of the Java collection classes stored Object references. When the time came to access references stored in a collection the Object reference retrieved had to be cast to the required interface. Such use of casting rendered code hard to read and proved difficult for novice programmers to master.

However, just because there is a new version of the API does not mean everybody switches to it right away. There is currently lots of new code being developed targeting Java platform version 1.4.x or earlier. Because of this I feel obligated to discuss the collections framework from two platform aspects: 1.4.x and 5.

I begin with a case study of a user-defined dynamic array class. The purpose of the case study is to provide a motivational context that demonstrates the need for a collections framework. I follow the case study with an overview of the collections framework, introducing you to its core interfaces and classes. I will then show you how to manipulate collections using Java platform 1.4.x programming techniques. You will find this treatment handy primarily because you may find yourself faced with maintaining legacy Java code. I will then discuss improvements made to the collections framework introduced with Java 5. This will include a discussion of static polymorphic behavior as it is realized by generics. I will also show you how to use the enhanced for loop and discuss autoboxing and unboxing of primitive types when being inserted into and retrieved from a collection.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

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