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 11.5 Arrays-WE2-P2
Subgoals for Evaluating Arrays.
Declaring and initialization of array
Set up a one dimensional table (i.e., one row) with 0 to (size - 1) elements
Upon instantiation of an array object, all elements contain default value for datatype stored in array OR values from the initializer list
Determine access or change of element, or action on entire array object, and update slots as needed (remembering assignment subgoals)
Evaluate expression within [] which will be the index for element to be accessed
arrayName[index]
returns value stored at that index
index must be between 0 and
arrayName.length
- 1, inclusive otherwise
IndexOutOfBounds
exception occurs
Changing value of an array element
Evaluate expression within [] which will be the index for element to be accessed
arrayName[index]
will now contain the value on the RHS of assignment statement
(remember the assignment subgoals for verifying data types and evaluating expressions)
(remember rules for index values)
Pass as argument - a copy of the reference to the instantiated array is passed to the method. This means that any changes made to the array elements inside the method are persistent. The one exception to this is if you assign the argument to reference a different array in memory.
Assignment - changes the reference to point to the array on the RHS of the assignment operator.
Subsection 11.5.1
Assume the following given declarations:
Give the contents of
beta
after the execution of these statements.
int size = 4;
beta = new int[size];
beta[0] = 50;
for (int i = 1; i < size; i++)
beta[i] = beta[i-1] - 2;
Exercises Exercises
1.
Q8: The value of
beta[0]
is
.
2.
Q9: The value of
beta[1]
is
.
3.
Q10: The value of
beta[2]
is
.
4.
Q11: The value of
beta[3]
is
.
You have attempted
of
activities on this page.