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 13.10 ArrayLists-WE5-P1
Subgoals for Evaluating ArrayLists.
Declaration and initialization of an ArrayList
Set up a one-dimensional table that will either be empty or have a specified initial capacity based on the parameter to the constructor
When declaring an ArrayList, the datatype stored in the container is specified inside of <>, and the data type must be the name of a class (no primitive data types)
Upon instantiation, an ArrayList contains zero elements initially, but elements can be added dynamically using
add()
Determine access or change of an element, or action on entire ArrayList object, and update elements as needed (remembering assignment subgoals)
Accessing an ArrayList element
Determine the value of the parameter in the
get(<expression>) method call (remember evaluating expressions subgoals)
The parameter to the method
get represents the index in the ArrayList. The size of the ArrayList is the number of elements contained. If the ArrayList is initially empty, the size is 0.
Index must be between 0 and
arrayListName.size() - 1, inclusive; otherwise
IndexOutOfBoundsException occurs
arrayListName.get(index) returns the value stored at that index
Changing value of an ArrayList element
Determine the value of the first parameter in the
set(<expression>, value) method call which will be the index for the element to be updated
arrayListName.set(index, value) replaces the element at
index with the specified
value
(remember the assignment subgoals for verifying data types and evaluating expressions)
(remember rules for index values)
Passing as argument - a copy of the reference to the instantiated ArrayList is passed to the method. This means that any changes made to the elements inside the method persist outside the method. The one exception to this is if you assign the argument to reference a different ArrayList in memory.
Assignment - changes the reference to point to the ArrayList on the right-hand side of the assignment operator.
Subsection 13.10.1
Exercises Exercises
1.
Q14: What does the following code accomplish? Assume
alpha is a correctly declared ArrayList with non-default values and that the variable
target contains a value.
int place = -1;
int index = 0;
for (int x : alpha) {
if (x == target)
place = index;
index += 1;
}
place contains the index of the first occurrence of
target within
alpha, -1 otherwise.
Incorrect.
place contains the index of the last occurrence of
target within
alpha, -1 otherwise.
Correct.
place always contains -1.
Incorrect.
the code "finds"
target within
alpha.
Incorrect.
the code does not work because you canβt get the index with an enhanced for loop.
Incorrect.
2.
Q15: What does the following code accomplish? Assume that
alpha is a correctly declared ArrayList with non-default values.
int value = alpha.get(alpha.size()-1);
for (int x = alpha.size()-2; x >= 0; x--) {
if (alpha.get(x) < value)
value = alpha.get(x);
}
finds the index of the first occurrence of the minimum value in
alpha
Incorrect.
finds the index of the last occurrence of the minimum value in
alpha
Incorrect.
finds the minimum value in
alpha
Correct.
finds the index of the first occurrence of the maximum value in
alpha
Incorrect.
finds the index of the last occurrence of the maximum value in
alpha
Incorrect.
3.
Q16: What does the following code accomplish? Assume that
alpha is a correctly declared ArrayList with non-default values.
int value = alpha.size()-1;
for (int x = alpha.size()-2; x >= 0; x--) {
if (alpha.get(x) > alpha.get(value))
value = x;
}
finds the index of the first occurrence of the minimum value in
alpha
Incorrect.
finds the index of the last occurrence of the minimum value in
alpha
Incorrect.
finds the minimum value in
alpha
Incorrect.
finds the index of the first occurrence of the maximum value in
alpha
Incorrect.
finds the index of the last occurrence of the maximum value in
alpha
Correct.
You have attempted
of
activities on this page.