📓4.9: ArrayList Traversals
Table of Contents
📖 This page is a condensed version of CSAwesome Topic 4.9
Traversing ArrayLists with Loops
🔁 ArrayLists can be traversed with while loops and both standard and enhanced for loops much the same way we use those constructs to loop over an array.
Enhanced For-Each Loop
You can use a enhanced for loop to traverse all of the items in an ArrayList, just like you do with an array when you only care about the values in the list and not their indices.
An example is shown below:
ArrayList<Integer> myList = new ArrayList<Integer>();
myList.add(50);
myList.add(30);
myList.add(20);
int total = 0;
for (Integer value : myList) {
total += value;
}
System.out.println("Sum of all elements: " + total);
💬 DISCUSS: What does the following code do? Guess before you run it. Then, add another enhanced for each loop that computes the product of all the elements in myList by multiplying them. Print out the product after the new loop.
Note however that you CANNOT use the enhanced for loop if you want to add or remove elements while traversing an ArrayList. If an ArrayList is modified, such as by calling the add or remove methods, while it is being looped over, it will cause the loop to throw a ConcurrentModificationException. If you need to modify an ArrayList while looping over it, you’ll need to use a regular while or for loop.
Enhanced for-each loops are often convenient but CANNOT be used in all situations! Only use for-each loops when you want to loop through all the values in an ArrayList, in sequential order, without making changes.
- 🚫 Do not use for-each loops if you need to modify values in the list.
- 🚫 Do not use for-each loops if you need to keep track of the current index.
- 🚫 Do not use for-each loops for non-sequential traversals (like iterating through only part of a list, or in a different order).
Standard For Loop
You can also use a while loop or a regular for loop to process list elements accessed using an index. ArrayList indices starts at 0 just like array indices, but instead of using the index operator [] to access elements, you use the get(index) method to get the value at the index and set(index,value) method to set the element at an index to a new value.
🚨 If you try to use an index that is outside of the range of 0 to the number of elements − 1 in an ArrayList, your code will throw an IndexOutOfBoundsException, similar to the ArrayIndexOutOfBoundsException thrown with Arrays.
for (int i = 0; i <= myList.size(); i++) {
total = total + myList.get(i);
}
System.out.println(total);
💬 DISCUSS: The code above will throw an
IndexOutOfBoundsException. Can you fix it?
While Loop
The example below demonstrates a while loop that removes a name from a list:
ArrayList<String> nameList = new ArrayList<String>();
nameList.add("Amun");
nameList.add("Lily");
nameList.add("Donnie");
nameList.add("Lily");
boolean found = // true or false?
int index = 0;
while (index < nameList.size()) {
if ("Lily".equals(nameList.get(index))) {
nameList.remove(index);
found = // true or false?
}
else {
index++;
}
}
Be careful when you remove items from a list while you loop through it! Notice how the method above only increments the
indexif an item was not removed from the list. This is because removing an item from a list will shift the remaining items to the left (lower index), so if you increment the index in all cases you will skip the elements immediately after each element you remove.
💻 In-Class Activity: FRQ Word Pairs
⭐️ Summary
- (AP 4.9.A.1) Traversing an
ArrayListis when iteration or recursive statements are used to access all or an ordered sequence of the elements in anArrayList. ArrayList\ s can be traversed with an enhancedforloop, awhileloop, or a regularforloop using an index.- (AP 4.9.A.2) Deleting elements during a traversal of an
ArrayListrequires the use of special techniques to avoid skipping elements (sinceremovemoves all the elements above the removed index down.) - (AP 4.9.A.3) Attempting to access an index value outside of its range will result in an
IndexOutOfBoundsException. (The indices for anArrayListstart at 0 and end at the number of elements − 1). - (AP 4.9.A.4) Changing the size of an
ArrayListwhile traversing it using an enhancedforloop can result in aConcurrentModificationException. Therefore, when using an enhancedforloop to traverse anArrayList, you should not add or remove elements.
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.