9.11.1. Arrays-WE9-P1ΒΆ
Subgoals for Evaluating Arrays
Set up array from 0 to size-1
Evaluate data type of statements against array
Trace statements, updating slots as you go
Remember assignment subgoals
Arrays-WE9-P1
- Inserts a new value into the first position of the array
- Inserts a new value into the middle position of the array
- Inserts a new value into the last position of the array
- deletes a value from a specific position of the array
- finds and deletes a value from the array
Q48: 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
- Inserts a new value into the middle position of the array
- Inserts a new value into the last position of the array
- deletes a value from a specific position of the array
- finds and deletes a value from the array
Q49: 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
- Inserts a new value into the middle position of the array
- Inserts a new value into the last position of the array
- deletes a value from a specific position of the array
- finds and deletes a value from the array
Q50: 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
- Inserts a new value into the middle position of the array
- Inserts a new value into the last position of the array
- deletes a value from a specific position of the array
- finds and deletes a value from the array
Q51: 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
- Inserts a new value into the middle position of the array
- Inserts a new value into the last position of the array
- deletes a value from a specific position of the array
- finds and deletes a value from the array
Q52: 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;
You have attempted of activities on this page