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.18.1
ExercisesExercises
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
place always contains -1
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 + " ");
}
5 10 15 20 25 30 35 40
6 11 16 21 26 31 36 41
1 2 3 4 5 6 7 8
6 10 15 20 25 30 35 40
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)
The code does not work
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;
---
}