The AP Computer Science A 5-hour live stream review is here!Β πŸ’»

Join us on May 5, 2021 for the 🌢️ AP Computer Science A Cram Finale for a last minute review to get all your questions answered!


All Subjects

Β >Β 


AP Comp Sci A

Β >Β 


Unit 7

7.3 Traversing ArrayLists

3 min readβ€’november 16, 2020

Peter Cao

Caroline Koffke

7.3: Traversing ArrayLists

Traversing ArrayLists

Now that we have made our ArrayList, it is time to traverse the ArrayList! To traverse an ArrayList, we can use the same two methods that we use to traverse regular arrays: using the regular for loop and the enhanced for loop. There are only two differences in the for loop implementation. The first is that instead of using bracket notation, we use get(). We also use size() for the ArrayList instead of the length variable. The enhanced for loop code is basically unchanged. Here is an example of each:
public static void forTraversal(ArrayList<E> list) { for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } public static void enhancedForTraversal(ArrayList<E> list) { for (E element: list) { System.out.println(element); } }
As with arrays, we can only change the items in an ArrayList using set(), and not by changing the enhanced for loop variable, as Java is a pass-by-value language. However, as before, we can change instance variables of objects inside ArrayLists using an enhanced for loop.

Traversing with Removing Elements

Sometimes we want to remove elements while traversing the ArrayList. This can only be done with the regular for loop. Changing the size of an ArrayList (either by adding or removing elements) while using an enhanced for loop will result in a ConcurrentModificationException. When deleting an element at an index i, remember that the element that used to be at index i+1 (the next element) is now at index i. This will require us to do an i-- before the loop incrementation to avoid skipping an element.
The size() in the loop condition will automatically change as you add or remove items, so we don't need to worry about it not being accurate or the method crashing. Here is a method that removes all even numbers from an ArrayList like integerList above:
/** Removes all even numbers */ public static ArrayList<Integer> removeEvens(ArrayList<Integer> list) { for (int i = 0; i < list.size(); i++) { if (list.get(i) % 2 == 0) { list.remove(i); i--; } } }

Traversing with Adding Elements

Other times, we may want to add elements to an ArrayList during traversal. However, this brings up its own problem. Let's take a look:
  1. Let's have a method that inserts a 1 at index i, if the element at i is equal to 4.
  2. At some index i, the element is 4, so 1 is now added at index i.
  3. That 4 is now at index i+1.
  4. i is incremented, so the current index being checked is i+1.
  5. At index i+1, the element is 4, so steps 2-4 are repeated, causing an infinite loop!
To avoid an infinite loop, we increment i before the loop incrementation so that i++ occurs twice and the loop checks the actual next item. Let's see this by duplicating all odd numbers in an ArrayList like integerList:
/** Duplicates all odd numbers */ public static ArrayList<Integer> duplicateOdds(ArrayList<Integer> list) { for (int i = 0; i < list.size(); i++) { if (list.get(i) % 2 == 1) { list.add(i, list.get(i)); i++; } } }

Was this guide helpful?

πŸ’ͺ🏽 Are you ready for the Comp Sci exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
FREE AP comp sci a Survival Pack + Cram Chart PDF
Sign up now for instant access to 2 amazing downloads to help you get a 5
Browse Study Guides By Unit
Exam Reviews
Unit 10: Recursion
Unit 1: Primitive Types
Unit 2: Using Objects
Unit 3: Boolean Expressions and if Statements
Unit 4: Iteration
Unit 5: Writing Classes
Unit 6: Array
Unit 8: 2D Array
Unit 9: Inheritance
Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now