Section 14.1 Worked Example: Writing ArrayLists - Storing Multiplication Table
Subgoals for Evaluating ArrayLists.
-
Importing the ArrayList class
-
Declaring an ArrayList variable
-
Determine the type of objects to be stored (use wrapper classes for primitives, e.g., Integer instead of int)
-
Determine the name of the ArrayList variable
-
Use syntax: ArrayList<DataType> name;
-
-
Instantiating an ArrayList object
-
Use the new keyword with the constructor to create a new ArrayList object
-
Assign to variable using: name = new ArrayList<>();
-
Optionally, initialize with values using: new ArrayList<>(List.of(value1, value2, ...)); (Java 9+)
-
-
Accessing an element in an ArrayList
-
Determine the index of the element to be accessed
-
Use: listName.get(index) to retrieve the element
-
Ensure the index is within bounds: 0 to listName.size() - 1, otherwise an IndexOutOfBoundsException occurs
-
-
Changing a value in an ArrayList
-
Determine the index of the element to be changed
-
Determine the new value or expression to assign
-
Use: listName.set(index, newValue) to update the value
-
-
Traversing an ArrayList
-
Decide whether accessing all elements, updating, or accessing a subset
-
If accessing only, use an enhanced for (for-each) loop:
-
for (DataType item : listName) - iterates from first to last, storing a copy of each element in item
-
-
If updating or using indices, use a traditional for loop:
-
Initialize loop control variable to 0 (or listName.size() - 1 for reverse)
-
Set condition: i < listName.size() (or i >= 0 for reverse)
-
Increment or decrement loop control variable appropriately
-
-
Use listName.get(i) to access or listName.set(i, newValue) to update
-
-
Whole list actions
-
Passing an ArrayList as an argument
-
Check if the method expects an ArrayList argument (check documentation or method signature)
-
Pass the ArrayList variable in the method call
-
Note: changes to the ArrayList in the method will persist (objects are passed by reference)
-
-
Reassigning an ArrayList
-
Determine that the reference to the list should point to a new ArrayList
-
Left-hand side is the variable name of the original list
-
Right-hand side is the new ArrayList, e.g., new ArrayList<>()
-
-
Subsection 14.1.1
Problem: Write the Java code to store the multiplication table for the value 13 using an ArrayList. The first element should be 0 (13 * 0), the second element should be 13 (13 * 1), the third element should be 26 (13 * 2), etc. There should be a total of 51 elements in the list.
Subsection 14.1.2 SG0: Importing ArrayList class
ArrayLists are not built-in like arrays and must be imported from the java.util package:
import java.util.ArrayList;
Subsection 14.1.3 SG1: Declaring an ArrayList variable
Because all multiples of 13 are integers, the datatype stored should be Integer (the wrapper class for int). We will name the variable mult13. The code to declare the ArrayList variable is:
ArrayList<Integer> mult13;
Subsection 14.1.4 SG2: Instantiating an ArrayList object
We want to store 51 elements in the list mult13. We can create the object as follows:
mult13 = new ArrayList<>();
Subsection 14.1.5 SG5: Traversing an ArrayList
Before accessing or updating elements, we need to populate the ArrayList. We will use a traditional for loop to add 51 elements, going from 0 to 50:
for (int x = 0; x < 51; x++)
Subsection 14.1.6 SG4: Adding values to the ArrayList
Inside the loop, we use the add() method to append each calculated multiple of 13 to the list:
mult13.add(x * 13);
Subsection 14.1.7
Answer.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> mult13;
mult13 = new ArrayList<>();
for (int x = 0; x < 51; x++) {
mult13.add(x * 13);
}
int i = 0;
for (int val : mult13) {
System.out.println("13 * " + i + " is " + val);
i++;
}
}
}
To verify we have the correct values, we print out each value in the list. Since we’re using an enhanced for loop (for-each), we track the multiplier using a separate variable i.
Note: Enhanced for loops do not provide direct access to index values, so we use a separate counter to track which multiple we’re printing.
Subsection 14.1.8 Practice Pages
You have attempted 1 of 2 activities on this page.