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.2 Writing-ArrayLists-WE1-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.2.1
Exercises Exercises
1.
Q1: Put the code in the appropriate order so that an
ArrayList
of integers will store multiples of 5 (from 100 down to 0) in reverse order.
import java.util.ArrayList;
---
public ArrayList<Integer> storeReverse() {
---
ArrayList<Integer> reverseFives = new ArrayList<>();
---
for (int i = 20; i >= 0; i--) {
---
reverseFives.add(i * 5);
---
}
---
return reverseFives;
---
}
2.
Q2. Which of the following would it be easier to use an initializer list for versus a loop when creating an
ArrayList
? (Select all that are appropriate)
Initialize an ArrayList of author names called
FavoriteAuthors
Initialize an ArrayList of numbers from 100 to 1000 called
NineHundred
Initialize an ArrayList of ribbon colors called
RibbonColors
Initialize an ArrayList with the top ten fastest times for your schoolβs track team in the 100 meter race named
TopTen
Initialize an ArrayList with the height (in inches) of all the children in a classroom called
ClassroomHeights
3.
Q3. Suppose you are writing a method that adds one to all of the elements of an
ArrayList
named
numbers
. Which of the following code snippets would be best for this task?
public void addOne(ArrayList<Integer> numbers) {
for (int i = 0; i < numbers.size(); i++) {
numbers.set(i, numbers.get(i) + 1);
}
}
public void addOne(ArrayList<Integer> numbers) {
for (int num : numbers) {
num++;
}
}
This does not modify the original list because it only changes a copy of each value.
public void addOne(ArrayList<Integer> numbers) {
for (int num : numbers) {
numbers.set(num, num + 1);
}
}
This misuses num
as both index and value.
public void addOne(ArrayList<Integer> numbers) {
for (int i = 0; i < numbers.size(); i++) {
numbers.set(i, i + 1);
}
}
This sets each element to its index plus one, not to the original value plus one.
You have attempted
of
activities on this page.