Skip to main content

Section 13.3 Worked Example: ArrayList - Traverse

Subgoals for Evaluating ArrayLists.

  1. Declaration and initialization of an ArrayList
    1. Set up a one-dimensional table that will either be empty or have a specified initial capacity based on the parameter to the constructor
    2. When declaring an ArrayList, the datatype stored in the container is specified inside of <>, and the data type must be the name of a class (no primitive data types)
    3. Upon instantiation, an ArrayList contains zero elements initially, but elements can be added dynamically using add()
  2. Determine access or change of an element, or action on entire ArrayList object, and update elements as needed (remembering assignment subgoals)
  3. Accessing an ArrayList element
    1. Determine the value of the parameter in the get(<expression>) method call (remember evaluating expressions subgoals)
    2. The parameter to the method get represents the index in the ArrayList. The size of the ArrayList is the number of elements contained. If the ArrayList is initially empty, the size is 0.
    3. Index must be between 0 and arrayListName.size() - 1, inclusive; otherwise IndexOutOfBoundsException occurs
    4. arrayListName.get(index) returns the value stored at that index
  4. Changing value of an ArrayList element
    1. Determine the value of the first parameter in the set(<expression>, value) method call which will be the index for the element to be updated
    2. arrayListName.set(index, value) replaces the element at index with the specified value
    3. (remember the assignment subgoals for verifying data types and evaluating expressions)
    4. (remember rules for index values)
  5. Whole ArrayList actions
    1. Passing as argument - a copy of the reference to the instantiated ArrayList is passed to the method. This means that any changes made to the elements inside the method persist outside the method. The one exception to this is if you assign the argument to reference a different ArrayList in memory.
    2. Assignment - changes the reference to point to the ArrayList on the right-hand side of the assignment operator.

Subsection 13.3.1

Problem: Given the following code, what is printed?
ArrayList<Integer> alpha = new ArrayList<Integer>();
for (int i = 0; i < 10; i++)
    alpha.add(i + i);
int sum = 0;
for (int x : alpha) {
    sum+=x;
}
System.out.println(sum);

Subsection 13.3.2 SG1: Declaring and Initialization of ArrayList

The first line will declare and instantiate the ArrayList alpha.
ArrayList<Integer> alpha = new ArrayList<Integer>();
  • alpha is declared as an ArrayList of Integers. Note that Integer is a wrapper class for an int, just converting a primitive into an object.
The next loop will initialize the values in the ArrayList alpha.
for (int i = 0; i < 10; i++)
    alpha.add(i + i);
  • This loop puts the values 0, 2, 4, 6, 8 into the ArrayList alpha by inserting each value at the end of the ArrayList.
  • The contents of alpha after this first loop completes are:
    [0, 2, 4, 6, 8]

Subsection 13.3.3 SG2: Determine Access or Action

for (int x : alpha){
    sum+=x;
}
Because ArrayList is a collection, we can use an enhanced for loop to traverse it.
Reminder, here’s how an enhanced for loop works:
for (dataType variable : collectionName){
    variable reference...
}
Within the for-each loop header you declare a local variable of the data type held in the collection followed by a colon (:) followed by the name of the collection. This tells the system to iterate through the collection, essentially doing a get call for each element and assigning the value from the collection to the variable. In the first iteration of the loop, the variable will hold the first element of the collection, in the second iteration, the variable will hold the second element of the collection, etc. Notice that you can reference the value of the ArrayList element (just the variable name), but you do not have access to the index or position of the element with an enhanced for loop.
Another important thing to remember is that the variable is a copy of the element, not the original element, so updates to the value of the variable have no effect on the values in the collection.
In this enhanced for loop we declare the loop variable as an int. It could have been declared as an Integer (the data type stored in the ArrayList) but because Java will automatically convert between an int and an Integer, it doesn’t actually matter in this case.

Subsection 13.3.4 SG3: Accessing an ArrayList Element

for (int x : alpha) {
    sum += x;
}
System.out.println(sum);
This code example is equivalent to the code used in the SectionΒ 13.7 example; it will sum the values of the ArrayList alpha.
The final value of sum is 20, which is the sum of all values in the ArrayList alpha
What is printed?
Answer.

Subsection 13.3.5 Practice Pages

You have attempted of activities on this page.