πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

πŸ’Ύ

7.3 Traversing ArrayLists

3 min readβ€’june 18, 2024

Milo Chang

Milo Chang

Avanish Gupta

Avanish Gupta


AP Computer Science AΒ πŸ’»

130Β resources
See Units

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.
  • Instead of using bracket notation, we use get().
  • We use size() for the ArrayList instead of the length variable.
Like with regular array traversals using for loops, if you try to access an index outside of the range of the ArrayList, you'll get a IndexOutOfBoundsException.
Here is an example of the for loop implementation:
public static void forTraversal(ArrayList<E> list) { for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } }
The enhanced for loop code is basically unchanged. Here is an example of the enhanced for loop implementation:
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 while 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 while 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++; } } }

Browse Study Guides By Unit
βž•Unit 1 – Primitive Types
πŸ“±Unit 2 – Using Objects
πŸ–₯Unit 3 – Boolean Expressions & if Statements
πŸ•ΉUnit 4 – Iteration
βš™οΈUnit 5 – Writing Classes
⌚️Unit 6 – Array
πŸ’ΎUnit 7 – ArrayList
πŸ’»Unit 8 – 2D Array
πŸ–²Unit 9 – Inheritance
πŸ–±Unit 10 – Recursion
🧐Exam Skills

Fiveable
Fiveable
Home
Stay Connected

Β© 2024 Fiveable Inc. All rights reserved.


Β© 2024 Fiveable Inc. All rights reserved.