Section 11.2 Arrays-WE1-P1
Subgoals for Evaluating Arrays.
-
Declaring and initialization of array
- 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 otherwiseIndexOutOfBounds
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.
Subsection 11.2.1
Exercises Exercises
2.
Q2: Assume the following declarations:
String [] alpha;
alpha = new String[4];
Evaluate these statements:
alpha[0] = "apple";
alpha[1] = "Banana";
alpha[2] = alpha[0].substring(2);
alpha[3] = alpha[alpha[1].indexOf('n')];
Give the contents of array
alpha
after the execution of the above statements:alpha[0] =
“”alpha[1] =
“”alpha[2] =
“”alpha[3] =
“”3.
Q3: If the following lines are compiled (in the order given), which line will generate the first compiler error?
- double [] gamma = new double[5];
- Notice that because the array
gamma
stores doubles, thengamma[0]
will return a double, which is not a valid data type for an index. It is the invalid index data type that causes the compiler error; the compiler does not look at stored array values. - gamma[0] = 14;
- Notice that because the array
gamma
stores doubles, thengamma[0]
will return a double, which is not a valid data type for an index. It is the invalid index data type that causes the compiler error; the compiler does not look at stored array values. - gamma[1] = gamma[0];
- Notice that because the array
gamma
stores doubles, thengamma[0]
will return a double, which is not a valid data type for an index. It is the invalid index data type that causes the compiler error; the compiler does not look at stored array values. - gamma[gamma[0]] = 42;
- Notice that because the array
gamma
stores doubles, thengamma[0]
will return a double, which is not a valid data type for an index. It is the invalid index data type that causes the compiler error; the compiler does not look at stored array values. - gamma[5] = 22;
- Notice that because the array
gamma
stores doubles, thengamma[0]
will return a double, which is not a valid data type for an index. It is the invalid index data type that causes the compiler error; the compiler does not look at stored array values.
You have attempted 1 of 3 activities on this page.