Section 14.3 Worked Example: Writing ArrayLists - Shifting Values
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.3.1
Problem: Write the Java code that will create an ArrayList of integers with the values 5, 10, 15, … 55 (multiples of 5). But then you remember you were supposed to start at 0, not 5 - so write the Java code that will shift all the values to the right by 1. In other words, the value 5 will shift from index 0 to index 1. And then you can put the value 0 at index 0.
Subsection 14.3.2 SG1: Declaring an ArrayList variable and SG2: Instantiating an ArrayList object
Because all multiples of 5 are integers, the datatype stored in the ArrayList should be
Integer
. We will name the variable alpha
. We also need to import the ArrayList
class and use the wrapper class Integer
for primitive type int
:
import java.util.ArrayList;
ArrayList<Integer> alpha = new ArrayList<Integer>();
Now, we’ll add the multiples of 5 from 5 to 55 using a loop:
for (int i = 5; i <= 55; i += 5) {
alpha.add(i);
}
Subsection 14.3.3 SG5: Traversing and Shifting an ArrayList
To shift the values to the right by one position, we must do a reverse traversal. We start from the last index and move each element to the next higher index. Then, we set the first index to 0:
for (int i = alpha.size() - 1; i > 0; i--) {
alpha.set(i, alpha.get(i - 1));
}
alpha.set(0, 0);
It’s important to go backwards so we don’t overwrite values before we’ve moved them. If we went forward, we’d end up copying the same value multiple times.
Answer.
import java.util.ArrayList;
ArrayList<Integer> alpha = new ArrayList<Integer>();
for (int i = 5; i <= 55; i += 5) {
alpha.add(i);
}
for (int i = alpha.size() - 1; i > 0; i--) {
alpha.set(i, alpha.get(i - 1));
}
alpha.set(0, 0);
Subsection 14.3.4 Practice Pages
You have attempted 1 of 2 activities on this page.