Section 14.1 Worked Example: Writing ArrayLists - Storing Multiplication Table
Subgoals for Writing 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 (When ArrayLists are instantiated, they are empty and have a size of 0.)
-
-
Adding elements to an ArrayList
-
To add to the end of an ArrayList, use:
listName.add(valueToBeAdded)
-
To add an element at a specific location in an ArrayList, use:
listName.add(index, valueToBeAdded)
where index is within the bounds 0 tolistName.size()
-
-
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 -
Ensure the index is within bounds: 0 to
listName.size() - 1
, otherwise an IndexOutOfBoundsException occurs
-
-
Traversing an ArrayList
-
Decide whether accessing all elements, updating, or accessing a subset
-
If only accessing elements, use an enhanced for (for-each) loop:
-
for (DataType item : listName)
- iterates from first to last, storing a copy of each element initem
-
-
If updating or using indices, use a traditional for loop:
-
Initialize loop control variable to 0 (or
listName.size() - 1
for reverse) -
Increment or decrement loop control variable appropriately
-
-
Use Subgoals 5 or 6 to access or change values as appropriate
-
-
Whole list actions
-
Passing an ArrayList as an argument
-
Check if the method expects an ArrayList argument (check documentation or method signature)
-
When calling a method, pass a reference to an ArrayList (usually variable name) as an argument in the method call.
-
Note: changes to elements in the ArrayList that are done inside the method will persist
-
-
Subsection 14.1.1
Problem: Write the Java code to store a student’s exam grades for a particular class for one term. We don’t know exactly how many exams there will be.
Subsection 14.1.2 SG1: Importing ArrayList class
Because we don’t know how many values we need to store, it is more appropriate to use an ArrayList which can dynamically grow to the size needed rather than an array where you might run out of space or have a lot of unused space.
ArrayLists are not built-in like arrays and must be imported from the java.util package:
import java.util.ArrayList;
Subsection 14.1.3 SG2: Declaring an ArrayList variable
The datatype stored in this ArrayList should probably be Double (the wrapper class for double) as exam grades may contain decimal values. We will name the variable
grades
. The code to declare the ArrayList variable is:
ArrayList<Double> grades;
Subsection 14.1.4 SG3: Instantiating an ArrayList object
We need to instantiate the ArrayList before using:
grades = new ArrayList<Double>();
Subsection 14.1.5 SG4: Adding values to the ArrayList
Use the
add()
method to append exam grades to the list:
grades.add(90.5);
grades.add(82);
grades.add(77.2);
grades.add(100);
Subsection 14.1.6 SG7: Traversing an ArrayList
To print the values in the ArrayList, we could traverse the list:
for (double d : grades)
System.out.println(d);
Or you can use the built-in
toString()
method from the ArrayList class:
System.out.println(grades); //toString automatically called
Subsection 14.1.7 SG6: Updating an element in an ArrayList
Use the
set()
method to update a specific exam grade in the list. Suppose the third exam grade has now been changed to 85:
grades.set(2, 85);
Remember that ArrayLists begin indexing at value 0, so the third exam is at index 2.
Subsection 14.1.8 SG5: Accessing an element in an ArrayList
To see the changed value, use
get()
:
System.out.println(grades.get(2));
Answer.
import java.util.ArrayList;
ArrayList<Double> grades;
grades = new ArrayList<Double>();
grades.add(90.5);
grades.add(82);
grades.add(77.2);
grades.add(100);
for (double d : grades)
System.out.println(d);
System.out.println(grades); //toString automatically called
grades.set(2, 85);
System.out.println(grades.get(2));
Subsection 14.1.9 Practice Pages
You have attempted 1 of 2 activities on this page.