Page 405 - Introduction to Programming with Java: A Problem Solving Approach
P. 405
10.2 ArrayBasics 371 10.13 ArrayListExampleUsingAnonymousObjectsandtheFor-EachLoop
10.14 ArrayListsVersusStandardArrays 10.1 Introduction
In the past, you’ve seen that objects typically contain more than one data item, and the different data items each have a different name. Now, we’ll look at a special kind of object that holds several items of the same type and uses the same name for all of them. Natural language has ways to give a single name to a popula- tion: “pack” of wolves, “herd” of cattle, “pride” of lions, “passel” of possum, “fesnying” of ferrets, and so on. Java has a way to do the same thing.
When you have a collection of items of the same type, and you’d like to use the same name for all of them, you can define them all together as an array. Each item in the array is more formally called an array element. To distinguish the different elements in the array, you use the array name plus a number that identi- fies the position of the element within the array. For example, if you stored a collection of song titles in an array named songs, you’d distinguish the first song title by saying songs[0], and you’d distinguish the second song title by saying songs[1]. As evidenced by this example, array elements start at position 0. An array’s position numbers (0, 1, 2, and so on) are more formally called indexes. We’ll have more to say about array indexes in the next section.
There’s an important advantage in using one name for all of a group of similar items and distinguishing
them only by a number. It can lead to simpler code. For example, if you need to store 100 song titles, you
could declare 100 separate variables. But what a pain it would be to have to write 100 declaration statements
Apago PDF Enhancer
and keep track of 100 different variable names. The easier solution is to use an array and declare just one
variable—a songs array variable.
Readers who want an early introduction to arrays have the option of reading Sections 10.1 through 10.6
after completing Chapter 4. The natural connection between Chapter 4 and this chapter is that Chapter 4 describes loops and arrays rely heavily on loops.
Starting with Section 10.7, we present arrays in an object-oriented context, where arrays are members of a class. We discuss techniques for searching an array and sorting an array. We describe different organizational structuresforarrays—two-dimensionalarraysandarraysofobjects.WethenpresentArray Lists,which are similar to arrays but provide more flexibility. ArrayLists grow dynamically as you add elements, and it’s easy to insert or delete elements in the middle of ArrayLists. Finally, we describe a special type of for loop called a for-each loop, which is particularly useful for processing the elements in an ArrayList.
10.2 Array Basics
In this section, we show you how to perform simple operations on an array, such as loading an array with data and printing an array. To illustrate these operations, we’ll refer to the phoneList array in Figure 10.1. The phoneList array holds a list of five speed-dial phone numbers for a cell phone. The first phone num- ber is 8167412000, the second phone number is 2024561111, and so on.
Accessing an Array’s Elements
To work with an array, you need to access an array’s elements. For example, to print the contents of an array, you need to access the array’s first element, print it, access the array’s second element, print it, and so on. To access an element within an array, you specify the array’s name, followed by square brackets surrounding the