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.18 Arrays-WE8-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.18.1
Exercises Exercises
1.
Q24: What does the following code accomplish? Assume that
alpha
is a correctly declared array 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
place
contains the index of the last occurrence of
target
within
alpha
, -1 otherwise
the code "finds"
target
within
alpha
the code does not work because you canβt get the index with an enhanced for loop
2.
Q25: What is the output of the following code?
int [] beta = {5, 10, 15, 20, 25, 30, 35, 40};
for (int x : beta) {
x += 1;
}
for (int x : beta) {
System.out.print(x + " ");
}
no output, the code does not work
3.
Q26: What does the following code accomplish? Assume that
alpha
is a correctly declared array with non-default values and that the variable
target
contains a value.
int max = -1000;
for (int x : alpha) {
if (x > max)
max = x;
}
max
contains the index of the maximum value within
alpha
(provided the max is > -1000)
max
contains the index of the maximum value within
alpha
max
contains the maximum value within
alpha
max
contains the maximum value within
alpha
(provided the max is > -1000)
4.
Q27: Put the following code in order to create a method that will return the average of the values in the array parameter.
public static double average (int [] arr) {
---
int sum = 0;
int size = arr.length;
---
if (size == 0)
---
return 0.0;
---
for (int i : arr)
---
sum += i;
---
return (sum + 0.0) / size;
---
}
You have attempted
of
activities on this page.