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 12.6 Writing-Arrays-WE3-P1
Subgoals for Writing Arrays.
Declaring an array variable
Determine data type to be stored
Data type followed by [] followed by name
Instantiating an array object
Determine size (number of elements to be stored)
Assign to variable: keyword new, followed by datatype, followed by [size]. (All values initialized to default value for datatype.)
Assign to variable: {initializer list} where initializer list contains the values to be stored in the array
Determine value of 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 an IndexOutOfBounds exception occurs at runtime
Changing value of an array element
Determine value of index of element to be changed (remember rules for index values)
Determine the expression for RHS (remember the assignment subgoals for verifying data types)
Write assignment statement to update array element
Decide if updating, accessing all in forward succession, or accessing some subset of array elements or accessing in a different order
If only
accessing elements , use an enhanced for (for-each) loop:
for (DataType varName : collectionName)
- traverses collectionName by iterating from first element to last element storing a copy of each element from collectionName in varName.
If updating or not accessing all elements in forward succession, write a for loop:
start loop control variable (which will also be index) at 0 to go forwards, (
arrayName.length - 1
) to go backwards
continuation test is loop control variable <
arrayName.length
for forwards, loop control variable >= 0 for backwards
update is loop control variable increments for forwards, decrements for backwards
inside loop use Subgoals 3 or 4 to access or change values as appropriate
Passing an array as an argument
Determine that the an entire array must be passed as an argument to a method by consulting documentation
When calling a method, pass a reference to an array (usually variable name) as an argument in the method call. (Remember that when passing an array as an argument that changes made by the method to the array are persistent.)
Subsection 12.6.1
Exercises Exercises
1.
Q7: What does the following code do?
int [] alpha = {10, 20, 30, 40, 50};
int [] beta = new int[alpha.length+1];
for (int i = 0; i < alpha.length; i++)
beta[i] = alpha[i];
beta[beta.length-1] = 42;
alpha = beta;
Inserts a new value into the first position of the array
Incorrect
Inserts a new value into the middle position of the array
Incorrect
Inserts a new value into the last position of the array
Correct
deletes a value from a specific position of the array
Incorrect
finds and deletes a value from the array
Incorrect
2.
Q8: What does the following code do?
int target = /* some value */ ;
int [] delta = {10, 20, 30, 40, 50, 60, 70, 80, 90};
boolean found = false;
int i, j;
for (i = 0; i < delta.length && !found; i++)
if (delta[i] == target)
found = true;
if (found) {
for (j = i-1; j < delta.length-1; j++)
delta[j] = delta[j+1];
delta[delta.length-1] = -999;
}
Inserts a new value into the first position of the array
Incorrect
Inserts a new value into the middle position of the array
Incorrect
Inserts a new value into the last position of the array
Incorrect
deletes a value from a specific position of the array
Incorrect
finds and deletes a value from the array
Correct
3.
Q9: What does the following code do?
int [] alpha = {10, 20, 30, 40, 50};
int [] beta = new int[alpha.length+1];
for (int i = 0; i < alpha.length; i++)
beta[i] = alpha[i];
beta[0] = 99;
alpha = beta;
Inserts a new value into the first position of the array
Correct
Inserts a new value into the middle position of the array
Incorrect
Inserts a new value into the last position of the array
Incorrect
deletes a value from a specific position of the array
Incorrect
finds and deletes a value from the array
Incorrect
4.
Q10: What does the following code do?
int pos = /* some value */;
int [] rho = {10, 20, 30, 40, 50, 60, 70, 80, 90};
for (int m = pos; m < rho.length-1; m++)
rho[m] = rho[m+1];
rho[rho.length-1] = -999;
Inserts a new value into the first position of the array
Incorrect
Inserts a new value into the middle position of the array
Incorrect
Inserts a new value into the last position of the array
Incorrect
deletes a value from a specific position of the array
Correct
finds and deletes a value from the array
Incorrect
5.
Q11: What does the following code do?
int [] alpha = {10, 20, 30, 40, 50};
int [] gamma = new int[alpha.length+1];
for (int i = 0; i < alpha.length; i++)
gamma[i] = alpha[i];
gamma[gamma.length/2] = 11;
alpha = gamma;
Inserts a new value into the first position of the array
Incorrect
Inserts a new value into the middle position of the array
Correct
Inserts a new value into the last position of the array
Incorrect
deletes a value from a specific position of the array
Incorrect
finds and deletes a value from the array
Incorrect
You have attempted
of
activities on this page.