Page 443 - Introduction to Programming with Java: A Problem Solving Approach
P. 443
constructor. Then it repeatedly prompts the user for a sales clerk name and sales value and calls the addSale method to insert the input data into the SalesClerks object. The looping stops when the user enters a q for the next name. Then main calls dumpData to display the accumulated sales data.
10.11 The ArrayList Class
As you’ve learned throughout this chapter, arrays allow you to work with an ordered list of related data. Ar- rays work great for many lists, but if you have a list where the number of elements is hard to predict, they don’t work so well. If you don’t know the number of elements, you have to either (1) start with an array size that’s large enough to accommodate the possibility of a very large number of elements or (2) create a new larger array whenever the array becomes full and you need more room for more elements. The first solution is wasteful of computer memory as it requires allocating space for a large array where most of the elements are unused. The second solution is what we did in the SalesClerks program’s doubleLength method. It works OK in terms of saving memory, but it requires the programmer to do extra work (writing the code that creates a larger array).
To help with lists where the number of elements is hard to predict, the folks at Sun came up with the ArrayList class. The ArrayList class is built using an array, but the array is hidden in the background, so you can’t access it directly. With an array in the background, the ArrayList class is able to provide the basic functionality that comes with a standard array. With its methods, the ArrayList class is able to provide additional functionality that helps when you don’t know the number of elements. In this section, we discuss how to create an ArrayList and how to use its methods.
H ow t o C r e a t e a n AAr rpaaygL ios t PDF Enhancer
The ArrayList class is defined in the Java API’s java.util package, so to use the class, you should
provide an import statement, like this: import java.util.ArrayList;
10.11 TheArrayListClass 409
To initialize an ArrayList reference variable, use this syntax:
ArrayList<element-type> reference-variable = new ArrayList<element-type>();
Note the angled brackets around element-type and reference-variable. The angled brackets are part of the required syntax. As indicated by the italics, element-type and reference-variable are descriptions. Nor- mally, we use angled brackets around such descriptions, but we’ll refrain from doing so when describing ArrayList syntax because description angled brackets might get confused with the ArrayList’s re- quired angled brackets. You should replace element-type with the type for the ArrayList’s elements. You should replace reference-variable with an actual reference variable. For example, suppose you’ve de- fined a Student class, and you want an ArrayList of Student objects. Here’s how to create such an ArrayList, named students:
Angled brackets are required.
ArrayList<Student> students = new ArrayList<Student>();
3 It’s legal to omit <element-type> when you use ArrayLists. If you omit it, then the ArrayList can store different types of ele- ments. That may sound exciting, but it’s not needed all that often. And there are drawbacks to omitting <element-type>:
a) It forces the programmer to use the cast operator when assigning an extracted element into a variable.
b) It eliminates type checking for assigning values into the ArrayList (since then it’s legal to assign any type).
3