Skip to main content
Logo image

Section 11.3 ArrayList Traversals

Traversing an ArrayList is when iteration (a loop) is used to access all or an ordered sequence of the elements in the ArrayList. Just like with arrays, we can use while loops, indexed for loops, or enhanced for loops to traverse an ArrayList. In a later lesson, we’ll also talk about recursion which is when a method calls itself over again which is another way you can traverse an array or ArrayList. In this lesson, we’ll focus on using loops.

Subsection 11.3.1 Enhanced For Loop

You can use an 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 in the main method below.
Note however that you can’t use the enhanced for loop if you want to add or remove elements while traversing an ArrayList. If the size of 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.

Activity 11.3.1.

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.

Activity 11.3.2.

The following has the correct code for the method getScore plus at least one extra unneeded code statement. This method will calculate and return the score for a word game. The code should loop through all of the elements in wordList and if the length of the current word is 3 it should add one to the score, if the length of the word is 4 it should add 2 to the score, and if the length is greater than 4 it should add 3 to the score. The method should return the score. Drag the needed blocks from the left into the correct order on the right. Check your solution by clicking on the Check button. You will be told if any of the blocks are in the wrong order or if you need to remove one or more blocks. There is one extra block that is not needed in a correct solution.

Subsection 11.3.2 For Loops and IndexOutOfBounds Exception

You can also use a regular indexed 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) 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 if you use the index operator on an array with an index out of bounds for that array.

Activity 11.3.3.

The following code will throw an IndexOutOfBoundsException. Can you fix it?

Subsection 11.3.3 While Loop

The example below demonstrates a while loop and an object-oriented approach where the list is a field of the current object and an instance method rather than a class (static) method loops through the list.

Activity 11.3.4.

The following code removes a name from a list. Set the found variable to the appropriate true or false values at line 13 and line 20 to make the code work.
Be careful when you remove items from a list as you loop through it. Notice how the method above only increments the index if 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 and if you increment the index in all cases you will skip the elements immediately after each element you remove. To see why, consider that those elements will be shifted into the position of the just removed element and if you increment the index, it will move to the next position, skipping the element that used to be at that position. Leaving the index unchanged after a remove allows the shifted-down element to be processed on the next time through the loop.

Activity 11.3.5.

Assume that nums has been created as an ArrayList object and it initially contains the following Integer values [0, 0, 4, 2, 5, 0, 3, 0]. What will nums contain as a result of executing numQuest?
ArrayList<Integer> list1 = new ArrayList<Integer>();
private ArrayList<Integer> nums;

// precondition: nums.size() > 0;
// nums contains Integer objects
public void numQuest()
{
   int k = 0;
   Integer zero = new Integer(0);
   while (k < nums.size())
   {
      if (nums.get(k).equals(zero))
         nums.remove(k);
      k++;
   }
}
  • [0, 4, 2, 5, 3]
  • Incrementing the index each time through the loop will miss when there are two zeros in a row.
  • [3, 5, 2, 4, 0, 0, 0, 0]
  • This would be true if the code moved the zeros to the end, but that is not what it does.
  • [0, 0, 0, 0, 4, 2, 5, 3]
  • This would be true if the code moved the zeros to the font, but that is not what it does.
  • [4, 2, 5, 3]
  • This would be correct if k was only incremented when an item was not removed from the list.
You can step through the code above by clicking on the following Example.

Activity 11.3.6.

The following has the correct code for a method called insertInOrder plus at least one extra unneeded code statement. This method should add the passed name in alphabetic order to a private list field called nameList. Drag the needed blocks from the left into the correct order on the right. Check your solution by clicking on the Check button. You will be told if any of the blocks are in the wrong order or if you need to remove one or more blocks. There is one extra block that is not needed in a correct solution.

Subsection 11.3.4 Reading in Files with java.nio.file

Although not covered in the AP CSA exam, the java.nio.file package (nio stands for new input/output), added in Java version 7, provides a better and easier way to read in files. The Files class in this package has a method called readAllLines that reads all of the lines in a file and returns them as a List of String objects. The readAllLines method throws an IOException if the file cannot be read. (List is an interface. Interfaces are not on the AP CSA exam but are quite common in actual Java programming. For now all you need to know is that all the methods we’ve talked about using with ArrayList you can use on any List such as the one returned by readAllLines.)
import java.nio.file.*;
...

// This connects and reads in the file in 1 line of code!
// It needs to be in a method that throws or handles IOException
List<String> lines = Files.readAllLines(Paths.get("data.txt"));
Under the covers readAllLines is almost certainly using an ArrayList which is a kind of List. The advantage of storing the lines in a dynamic data structure like an ArrayList, instead of an array, is that you do not need to know how many lines you are going to store when you create the ArrayList the way you do when you create an array. The ArrayList can then grow in size as needed. (If you absolutely need an array, you can convert the List to an array declared to be the correct size with myArray = lines.toArray(myArray);)

Activity 11.3.7.

Complete the code in the main method below to reads all lines of the file using Files.readAllLines into a List<String> named lines. Add a loop that prints out the first 10 pokemon.

Subsection 11.3.5 Traversing an ArrayList of Student Objects

You can put any kind of objects into an ArrayList. For example, here is an ArrayList of Students. Let’s write some loops that traverse the ArrayList to print out each Student by implicitly calling its toString() method. We’ll also write a method that filters the ArrayList to print out only the students who have a GPA higher than 3.5 for the honor roll.
Method calls can be chained together like the following:
// get the name of the 0's student
String name = students.get(0).getName();
// get the GPA of the student at index i
double gpa = students.get(i).getGPA();

Activity 11.3.8.

Write the methods printRoster() and honorRoll() for the ArrayList of Student objects below.

Subsection 11.3.6 Coding Challenge: FRQ Word Pairs

This challenge is based on the 2018 Free Response Question #2 WordPair. We encourage you to work in pairs on this challenge.
You are given a class called WordPair that can store pairs of words.
class WordPair
{
    private String word1;
    private String word2;

    public WordPair(String word1, String word2)
    {
        this.word1 = word1;
        this.word2 = word2;
    }

    public String getFirst()
    {
        return word1;
    }

    public String getSecond()
    {
        return word2;
    }

    public String toString()
    {
        return "(" + word1 + ", " + word2 + ")";
    }
}
First, see if you can create an ArrayList of WordPair objects below. Look at the StudentList example above for help.

Activity 11.3.9.

Create an Arraylist of WordPair objects.
Figure 11.3.1.
In this FRQ, you are given an array of words and you will create pairs of them by taking the first word and pairing it with all the other words, then taking the second word and pairing it with all but the first one, and so on. For example, if the word array is [β€œHi”, β€œthere”, β€œTyler”, β€œSam”], this figure shows how the word pairs are formed.
In the class WordPairsList below, you will write the constructor which takes the array of words and pairs them up as shown in the figure. You will need nested loops to pair each element with the rest of the elements in the list.
Here is the pseudocode for the constructor method.
  • Initialize the allPairs list to an empty ArrayList of WordPair objects.
  • Loop through the words array for the first word in the word pair (for loop from index i = 0 to length-1)
    • Loop through the rest of the word array starting from index i + 1 for the second word in the word pair (for loop from index j = i + 1 to length)
      • Add the new WordPair formed from the ith word and the jth word to the allPairs ArrayList.

Project 11.3.10.

FRQ WordPairs Challenge: Complete the constructor for WordPairsList below which will add pairs of words from a given array to the ArrayList. Then, complete the method numMatches() as described below this exercise.
In the next part of the FRQ challenge, you are asked to write a method called numMatches that counts and returns the number of pairs where the first word is the same as the second word. For example, if the word array is ["hi","bye","hi"], the pairs generated would be ["hi","bye"], ["hi","hi"], and ["bye","hi"]. In the second pair ["hi","hi"], the first word is the same as the second word, so numMatches would return 1.
For this method, you will need a loop that goes through the ArrayList allPairs and for each WordPair in allPairs, it checks to see if its first word (using the getFirst method) equals the second word (using the getSecond method). If there is a match, it increments a counter which it returns at the end of the method. To test this method, add another β€œthere” into the words array and then uncomment the call to numMatches.

Subsection 11.3.7 Summary

  • (AP 4.9.A.1) Traversing an ArrayList is when iteration or recursive statements are used to access all or an ordered sequence of the elements in an ArrayList.
  • ArrayLists can be traversed with an enhanced for loop, a while loop, or a regular for loop using an index.
  • (AP 4.9.A.2) Deleting elements during a traversal of an ArrayList requires the use of special techniques to avoid skipping elements (since remove moves 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 an ArrayList start at 0 and end at the number of elements βˆ’ 1).
  • (AP 4.9.A.4) Changing the size of an ArrayList while traversing it using an enhanced for loop can result in a ConcurrentModificationException. Therefore, when using an enhanced for loop to traverse an ArrayList, you should not add or remove elements.
You have attempted of activities on this page.