Data Structures

Iterating over an ArrayList

Earlier in this lesson, we learned about the for loop. Since we've also learned how to retrieve the value at a specific index of an ArrayList, we can now access each of the elements.

for (int i = 0; i < quizGrades.size(); i++) {

    System.out.println( quizGrades.get(i) );


In the example above, the for loop above includes the following statements:

  1. Initialization: int variable i is set to 0 which is the first index of an ArrayList.
  2. Test condition: the code in the block will run as long as i is less than the size of quizGrades.
  3. Increment: The code in this block will execute after each loop. In this case,i will increment by 1 with i++ after each loop.

The size method returns an int that represents how many total elements are stored within quizGrades. The example will print out each element within quizGrades in order. The process of going through each element in the ArrayList is called iteration.

Community Forums
Get help and ask questions in the Codecademy Forums
Report a Bug
If you see a bug or any other issue with this page, please report it here.