This chapter, when boiled down to its essence, is about data structures. Specifically, it is about arrays—the java.util.Arrays
and java.util.ArrayList
classes. An array is a region of memory that can be addressed using a single variable name. It provides an efficient technique for accessing data in a sequential or random fashion. The Arrays
class provides support for arrays while the ArrayList
class provides array-like behavior but is not fixed in size.
We are concerned with how to create and use these data structures. A common operation is the traversal of an array or collection. We will see that Java supports several approaches permitting us to move through the elements of an array or an ArrayList
object. Common to both arrays and collections is the ability to use the for-each statement. Iterators provide an alternate approach for accessing collections such as the ArrayList
, and will also be discussed.
We will start by examining arrays in detail...