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)
Accessing array element
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)
Whole array actions
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.
Subsection11.10.1
ExercisesExercises
1.
Q16: Given the following code, what is the value in sum after execution?
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];
2.
Q17: Given the following code, what is the value in sum after execution?
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];