Skip to main content
Contents
Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 14.6 Writing-ArrayLists-WE3-P1
Subgoals for Evaluating ArrayLists.
Importing the ArrayList class
Before using ArrayList, import it from the java.util package:
import java.util.ArrayList;
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
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
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)
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.6.1
Exercises Exercises
1.
Q7: What does the following code do?
ArrayList<Integer> alpha = new ArrayList<>(Arrays.asList(10, 20, 30, 40, 50));
ArrayList<Integer> beta = new ArrayList<>(alpha);
beta.add(42);
alpha = beta;
Inserts a new value into the first position of the list
Inserts a new value into the middle position of the list
Inserts a new value into the last position of the list
Deletes a value from a specific position of the list
Finds and deletes a value from the list
2.
Q8: What does the following code do?
int target = /* some value */ ;
ArrayList<Integer> delta = new ArrayList<>(Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80, 90));
boolean found = false;
int i;
for (i = 0; i < delta.size() && !found; i++) {
if (delta.get(i) == target) {
found = true;
}
}
if (found) {
delta.remove(i - 1);
delta.add(-999);
}
Inserts a new value into the first position of the list
Inserts a new value into the middle position of the list
Inserts a new value into the last position of the list
Deletes a value from a specific position of the list
Finds and deletes a value from the list
3.
Q9: What does the following code do?
ArrayList<Integer> alpha = new ArrayList<>(Arrays.asList(10, 20, 30, 40, 50));
ArrayList<Integer> beta = new ArrayList<>();
beta.add(99);
beta.addAll(alpha);
alpha = beta;
Inserts a new value into the first position of the list
Inserts a new value into the middle position of the list
Inserts a new value into the last position of the list
Deletes a value from a specific position of the list
Finds and deletes a value from the list
4.
Q10: What does the following code do?
int pos = /* some value */ ;
ArrayList<Integer> rho = new ArrayList<>(Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80, 90));
rho.remove(pos);
rho.add(-999);
Inserts a new value into the first position of the list
Inserts a new value into the middle position of the list
Inserts a new value into the last position of the list
Deletes a value from a specific position of the list
Finds and deletes a value from the list
5.
Q11: What does the following code do?
ArrayList<Integer> alpha = new ArrayList<>(Arrays.asList(10, 20, 30, 40, 50));
ArrayList<Integer> gamma = new ArrayList<>(alpha);
gamma.add(gamma.size() / 2, 11);
alpha = gamma;
Inserts a new value into the first position of the list
Inserts a new value into the middle position of the list
Inserts a new value into the last position of the list
Deletes a value from a specific position of the list
Finds and deletes a value from the list
You have attempted
of
activities on this page.