Page 451 - Introduction to Programming with Java: A Problem Solving Approach
P. 451

                10.13 ArrayList Example Using Anonymous Objects and the For-Each Loop 417
a reference to a Double wrapper object (since stocks is declared to be an ArrayList of Double ref- erences). When the JVM sees a primitive value argument (stock), it automatically wraps the argument in a Double wrapper class.
More formally, unboxing is the process of automatically extracting a primitive value from a wrapper object whenever there’s an attempt to use a wrapper object in a place that expects a primitive. Refer to the stock = stocks.get(i);statementinFigure10.27.Thatstatementcausesunboxingtooccur.Since stock is a primitive variable, the JVM expects a primitive value to be assigned into it. When the JVM sees a wrapper object on the right of the assignment statement (stocks holds Double wrapper objects and get(i) retrieves the ith such wrapper object), it automatically extracts the primitive value from the wrapper object.
Autoboxing and unboxing take place automatically behind the scenes. That makes the programmer’s job easier. Yeah!
10.13 ArrayList Example Using Anonymous Objects and the For-Each Loop
Anonymous objects and for-each loops are programming constructs that are particularly useful when used in conjunction with ArrayLists. In this section, we present for-each loop details and anonymous object details by showing how they’re used in the context of an ArrayList program. But before we get to the program, we provide brief introductions for the two new constructs.
Usually, when you create an object, you immediately store the object’s reference in a reference variable.
A for-each loop is a modified version of the traditional for loop. It can be used whenever there’s a need to iterate through all of the elements in a collection of data. An ArrayList is a collection of data, and, as such, for-each loops can be used to iterate through all of the elements in an ArrayList.
A Bear-Store Example
Suppose you want to model a store which sells customized toy bears. You need a Bear class to represent each bear, a BearStore class to represent the store, and a BearStoreDriver class to “drive” the pro- gram. Let’s start by examining the Bear class in Figure 10.28. The Bear class defines two instance named constants which represent two permanent properties of a particular bear: (1) MAKER, the bear’s manufac- turer, such as Gund, and (2) TYPE, the bear’s type, such as “pooh bear” or “angry campground bear.” A constructor initializes these two instance constants, and a display method displays them.
Now let’s examine the first part of the BearStore class, shown in Figure 10.29a. The BearStore class has one instance variable, bears, which is declared to be an ArrayList of Bear references. It holds the store’s collection of toy bears. The BearStore class’s addStdBears method fills the bears ArrayList with a specified number of standard teddy bears. Here’s the statement that adds one standard teddy bear to the ArrayList:
bears.add(new Bear("Acme", "brown teddy"));
The statement instantiates a Bear object and passes the Bear object’s reference to the bears. add method call. The statement does not assign the Bear object’s reference to a Bear reference vari-
 Apago PDF Enhancer
That way, you can refer to the object later on by using the reference variable’s name. If you create an object and don’t immediately assign the object’s reference to a reference variable, you’ve created an anonymous object. It’s called anonymous because it doesn’t have a name.




















































































   449   450   451   452   453