Skip to main content

Section 11.2 Arrays-WE1-P1

Subgoals for Evaluating Arrays.

  1. Declaring and initialization of array
    1. Set up a one dimensional table (i.e., one row) with 0 to (size - 1) elements
    2. Upon instantiation of an array object, all elements contain default value for datatype stored in array OR values from the initializer list
  2. Determine access or change of element, or action on entire array object, and update slots as needed (remembering assignment subgoals)
  3. Accessing array element
    1. Evaluate expression within [] which will be the index for element to be accessed
    2. arrayName[index] returns value stored at that index
    3. index must be between 0 and arrayName.length - 1, inclusive otherwise IndexOutOfBounds exception occurs
  4. Changing value of an array element
    1. Evaluate expression within [] which will be the index for element to be accessed
    2. arrayName[index] will now contain the value on the RHS of assignment statement
    3. (remember the assignment subgoals for verifying data types and evaluating expressions)
    4. (remember rules for index values)
  5. Whole array actions
    1. 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.
    2. Assignment - changes the reference to point to the array on the RHS of the assignment operator.

Subsection 11.2.1

Exercises Exercises

1.
Q1: Assume the following declarations:
int [] beta;
beta = new int[3];
Evaluate these statements:
beta[1] = 22;
beta[0] = beta[1] - 11;
beta[2] = beta[0] + beta[1];
Give the contents of array beta after the execution of the above statements:
beta[0] =
beta[1] =
beta[2] =
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, then gamma[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, then gamma[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, then gamma[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, then gamma[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, then gamma[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 of activities on this page.