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

                412 Chapter 10 Arrays and ArrayLists
In the set method’s API heading, the index parameter specifies the position of the element you’re interested in. If index refers to a nonexistent element, then a runtime error occurs. If index is valid, then set assigns the elem parameter to the specified element. Note that elem is declared with E for its type. As with the set method, the E represents the data type of the ArrayList’s elements. So elem is the same type as the type of ArrayList’s elements. This example illustrates what we’re talking about:
String mixedColor;
ArrayList<String> colors = new ArrayList<String>();
colors.add("red");
colors.add("green");
colors.add("blue");
mixedColor = colors.get(0) + colors.get(1);
colors.set(2, mixedColor);
Note that mixedColor is declared to be a string and colors is declared to be an ArrayList of strings. So in the last statement when we use mixedColor as the second argument in the set method call, the argument is indeed the same type as the type of color’s elements.
Can you determine what the colors ArrayList looks like after the code fragment executes? Draw a picture of the colors ArrayList on your own before proceeding. When you’re done, compare your answer to this:
colors
Apago0 PD“rFed” Enhancer
1 2
In the set method’s API heading, note the return type, E. Most mutator/set methods simply assign a value and that’s it. In addition to assigning a value, the ArrayList’s set method also returns a value—the value of the specified element prior to the element being updated. Usually, there’s no need to do anything with the original value, so you just call set and the returned value dies. That’s what happens in the above code fragment. But if you want to do something with the original value, it’s easy to get it because set returns it.
Additional ArrayList Methods
We’ve now explained the most important methods for the ArrayList class. There are quite a few more methods, and Figure 10.25 provides API headings and brief descriptions for five of them. As you read through the figure, we hope that you’ll find most of the methods to be straightforward. But some items may need clarification. In searching an ArrayList for the first occurrence of a passed-in elem parameter, the indexOf method declares elem’s type to be Object. The Object type means the parameter may be any kind of object. Naturally, if the parameter’s actual type is different from the type of elements in the ArrayList, then indexOf’s search comes up empty and it returns 􏰂1 to indicate that elem was not found. By the way, we’ll have lots more to say about the Object type (it’s actually an Object class) in Chapter 13. Previously, we covered a one-parameter add method that adds an element at the end of the ArrayList. Figure 10.25’s overloaded two-parameter add method adds an element at a specified position within the ArrayList.
  “green”
 “redgreen”
















































































   444   445   446   447   448