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 11.10 Arrays-WE4-P1
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.10.1
Exercises Exercises
int [] alpha = {2, 4, 6, 8, 10, 12, 14};
int sum = 0;
for (int k = 0; k < alpha.length; k+=2)
sum = sum + alpha[k];
1.
int [] beta = new int [5];
int sum = 0;
for (int k = 0; k < beta.length; k++)
beta[k] = 2 * k + 1;
int x = 3;
for (int k = 1; k < beta.length; k++)
sum += beta[k];
2.
3.
Q18: Look at the code below:
int [] alpha = {2, 4, 6, 8, 10, 12, 14};
for (int k = 0; k < alpha.length; k+=2) {
int sum = 0;
sum = sum + alpha[k];
}
System.out.println(sum);
What is the output of the above code?
There is a compiler error so the code does not run. The variable sum is not in scope for the println statement.
Correct
The output is 0 because the variable is re-initialized every time in the loop.
Incorrect
The output is 14 because the variable sum only contains the last value in the array.
Incorrect
The output is 56 which is the sum of all the values in the array.
Incorrect
4.
Q19: Look at the code below:
int [] alpha = {2, 4, 6, 8, 10, 12, 14};
int sum;
for (int k = 0; k < alpha.length; k+=2) {
sum = 0;
sum = sum + alpha[k];
}
System.out.println(sum);
What is the output of the above code?
There is a compiler error so the code does not run. The variable sum is not in scope for the println statement.
Incorrect
The output is 0 because the variable is re-initialized every time in the loop.
Incorrect
The output is 14 because the variable sum only contains the last value in the array.
Correct
The output is 56 which is the sum of all the values in the array.
Incorrect
You have attempted
of
activities on this page.