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 13.8 Assessment: ArrayLists 1
Subgoals for Evaluating ArrayLists.
Declaring and initialization of an ArrayList
Set up a one-dimensional dynamic list (initially empty or with a specified initial capacity)
Upon instantiation, an ArrayList contains zero elements initially, but elements can be added dynamically using
add()
. Elements not yet added do not exist until explicitly inserted.
Determine access or change of element, or action on entire ArrayList object, and update elements as needed (remembering assignment subgoals)
Accessing an ArrayList element
Evaluate expression within
get(index)
which will be the index for the element to be accessed
arrayListName.get(index)
returns the value stored at that index
index must be between 0 and
arrayListName.size() - 1
, inclusive; otherwise
IndexOutOfBoundsException
occurs
Changing value of an ArrayList element
Evaluate expression within
set(index, value)
which will be the index for the element to be replaced
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 exception is if the argument is assigned to reference a different ArrayList inside the method.
Assignment - changes the reference to point to the ArrayList on the right-hand side of the assignment operator.
Exercises Exercises
1.
Q1: Assuming that the following declaration has been made, which of the following code segments correctly interchanges the value of
arr.get(0)
and
arr.get(5)
?
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(5, 12, -22, 6, 11, 0, 26, 42, 99, 75));
arr.set(0, 5); arr.set(5, 0);
arr.set(0, arr.get(5)); arr.set(5, arr.get(0));
int k = arr.get(5); arr.set(0, arr.get(5)); arr.set(5, k);
int k = arr.get(0); arr.set(0, arr.get(5)); arr.set(5, k);
int k = arr.get(5); arr.set(5, arr.get(0)); arr.set(0, arr.get(5));
2.
Q2: Consider the following code that is intended to print
true
if all the elements in
arr
are even numbers; otherwise it should print
false
. You may assume that
arr
has been declared and contains valid integer values.
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(2, 4, 6, 8, 10));
boolean isEven = /* expression */;
for (int k = 0; k < arr.size(); k++) {
/* loop body */
}
if (isEven)
System.out.println("TRUE");
else
System.out.println("FALSE");
Which of the following replacements for
/* expression */
and
/* loop body */
should be used so that the code works as intended?
/* expression */: false /* loop body */: if ((arr.get(k) % 2) == 0) isEven = true;
/* expression */: false /* loop body */: if ((arr.get(k) % 2) != 0) isEven = false; else isEven = true;
/* expression */: true /* loop body */: if ((arr.get(k) % 2) != 0) isEven = false;
/* expression */: true /* loop body */: if ((arr.get(k) % 2) != 0) isEven = false; else isEven = true;
/* expression */: true /* loop body */: if ((arr.get(k) % 2) == 0) isEven = false; else isEven = true;
3.
Q3: Considering the following code, what are the values in
numbers
after execution?
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(17, 34, 21, 42, 15, 69, 48, 25, 39));
int x = 3;
for (int k = 1; k < 9; k = k + x)
numbers.set(k, numbers.get(k - 1) + x);
<17, 20, 21, 42, 45, 69, 48, 51, 39>
<17, 20, 23, 26, 29, 32, 35, 38, 41>
<17, 37, 21, 42, 18, 69, 48, 28, 39>
<20, 23, 21, 42, 45, 69, 51, 54, 39>
<20, 34, 21, 45, 15, 69, 51, 25, 39>
You have attempted
of
activities on this page.