Page 456 - Introduction to Programming with Java: A Problem Solving Approach
P. 456
422 Chapter 10 Arrays and ArrayLists
The for-each loop is great, but you should be aware of several issues when using it. (1) It was introduced in Java 5.0, so it won’t work with older compilers. (2) The for-each loop doesn’t use an index variable to loop through its elements. That can be a benefit in that it leads to less cluttered code. But it’s a drawback if there’s a need for an index within the loop. For example, suppose you’re given a primes array, like above, and you want to print this:
primes[0] = 1
primes[1] = 2
...
primes[5] = 11
The numbers inside the square brackets are index values. So if you implemented a solution with a for-each loop, you’d have to add an index variable to your code and increment it each time through the loop. On the other hand, if you implemented a solution with a traditional for loop, you’d already have an incrementing index variable built in.
10.14 ArrayLists Versus Standard Arrays
There’s a lot of overlap in the functionality of an ArrayList and a standard array. So how can you tell which one to use? Your answer will be different for different situations. When deciding on an implementa- tion, consider this table:
Apago PDF Enhancer
Benefits of an ArrayList Over a Standard Array
Benefits of a Standard Array Over an ArrayList
1. It’s easy to increase the size of an ArrayList— just call add.
2. It’s easy for a programmer to insert or remove an element to or from the interior of an ArrayList—just call add or remove and specify the element’s index position.
1. A standard array uses [ ]’s to access array elements (which is easier than using get and set methods).
2. A standard array is more efficient when storing primitive values.
In looking at the table’s first ArrayList benefit, easy to increase the size of an ArrayList, think about how much work is required to increase the size of a standard array. For a standard array, the programmer needs to instantiate a larger array and then copy the old array’s contents to the new larger array. On the other hand, for an ArrayList, the programmer simply needs to call the add method. Note that behind the scenes, the JVM has to put forth some effort in implementing the add method, but the effort is kept to a minimum. ArrayLists are implemented with the help of an underlying standard array. Usually, the underlying array has a greater number of elements than the ArrayList, so adding another element to the ArrayList is easy—the JVM just borrows an unused element from the underlying array. As a program- mer, you don’t have to worry about or code those details; the “borrowing” takes place automatically.
The table’s second ArrayList benefit, easy for a programmer to insert or remove an element to or from the interior of an ArrayList, is true, but just because it’s easy for programmers doesn’t mean it’s easy for the JVM. Actually, the JVM has to do quite a bit of work when it adds or removes from the inte- rior of an ArrayList. To insert an element, the JVM has to adjust its underlying array by shifting higher indexed elements to make room for the new element. And to remove an element, the JVM has to adjust its underlying array by shifting higher indexed elements to overlay the removed element.