Skip to main content
Contents
Dark Mode 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 Writing 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 (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 to
listName.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
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 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 Subgoals 5 or 6 to access or change values as appropriate
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.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<Integer>();
---
for (int i = 20; i >= 0; i--) {
---
reverseFives.add(i * 5);
---
}
---
return reverseFives;
---
}
2.
Q2: Which of the following should use an ArrayList rather than an array? (Select all that are appropriate)
Store numbers from 100 to 1000
Incorrect
Store a list of favorite authors
Correct
Store the top ten fastest times for your schoolβs track team in the 100 meter race
Incorrect
Store the incoming fast food restaurant orders
Correct
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);
}
}
Correct
public void addOne(ArrayList<Integer> numbers) {
for (int num : numbers) {
num++;
}
}
Incorrect
public void addOne(ArrayList<Integer> numbers) {
for (int num : numbers) {
numbers.set(num, num + 1);
}
}
Incorrect
public void addOne(ArrayList<Integer> numbers) {
for (int i = 0; i < numbers.size(); i++) {
numbers.set(i, i + 1);
}
}
Incorrect
You have attempted
of
activities on this page.