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
Accessing array element
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
Traversing an array
Decide if updating, accessing all in forward succession, or accessing some subset of array elements or accessing in a different order
If accessing only, write an enhanced for (for each) loop:
for (DataType varName : collectionName) - traverses collectionName from first element to last element storing a copy of each element from collectionName in varName for each iteration of the loop.
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 access / update array element using loop control variable as index into array or varName has value of array element
Whole array actions
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, put variable name that represents the array 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.)
Array Assignment
Determine that the reference to an array needs to be changed
The LHS of the assignment is the array reference needing to be changed
The RHS of the assignment is the new array reference
Subsection12.4.1
ExercisesExercises
1.
Q45: What are the contents of array alpha after this code has been executed?
int [] alpha = {10, 20, 30, 40, 50, 60, 70};
int start = alpha[alpha.length-1];
for (int i = 1; i < alpha.length; i++) {
alpha[i] = alpha[i-1];
}
alpha[0] = start;
{20, 30, 40, 50, 60, 70, 10}
{10, 20, 30, 40, 50, 60, 70}
{10, 10, 20, 30, 40, 50, 60}
{70, 10, 10, 10, 10, 10, 10}
{10, 10, 10, 10, 10, 10, 10}
2.
Q46: What are the contents of array beta after this code has been executed?
int [] beta = {10, 20, 30, 40, 50, 60, 70};
for (int i = 1; i < beta.length; i++) {
beta[i] = beta[i-1];
}
{20, 30, 40, 50, 60, 70, 10}
{10, 20, 30, 40, 50, 60, 70}
{10, 10, 20, 30, 40, 50, 60}
{70, 10, 20, 30, 40, 50, 60}
{10, 10, 10, 10, 10, 10, 10}
3.
Q47: What are the contents of array gamma after this code has been executed?
int [] gamma = {10, 20, 30, 40, 50, 60, 70};
for (int i = 0; i < gamma.length; i++) {
gamma[i] = gamma[i+1];
}