Skip to main content

Section 11.1 Worked Example: Arrays - Instantiate and Alter

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.1.1

Problem:
Given the initialized array:
int [] alpha;
Evaluate these statements and determine the value of all variables. If any error occurs, give the reason.
alpha = new int[5];
alpha[4] = 22;
alpha[0] = 10;
alpha[1] = alpha[4] - alpha[0];
alpha[2] = alpha[1] - alpha[0];
alpha[3] = alpha[alpha[2] - 1];
alpha[4] = alpha[alpha[3]];

Subsection 11.1.2 SG1: Declaration and initialization of array

alpha = new int[5];
Figure 11.1.1.
  • alpha is declared as an array of ints
  • This statement allocates 5 slots for integers (first line are indexes, second line are values/content):
  • The declared size of array is 5, so last valid index value is 4
  • Notice that the array contains values of 0 because 0 is the default value for integers, the data type stored in the array.

Subsection 11.1.3 SG2: Determine access or action

alpha[4] = 22;
alpha[0] = 10;
alpha[1] = alpha[4] - alpha[0];
alpha[2] = alpha[1] - alpha[0];
alpha[3] = alpha[alpha[2] - 1];
alpha[4] = alpha[alpha[3]];
In this example, whenever alpha[index] is used on the LHS of an assignment statement, then we will be updating the array element. When alpha[index] is used on the RHS of an assignment statement, then we are accessing an element.
For each of the lines in the example code, we will walk through the evaluation steps indicating the appropriate subgoals.

Subsection 11.1.4

alpha[4] = 22;
alpha[0] = 10;
For the first statement, we will be changing the value of an array element (SG4)
First, we evaluate the expression within [ ], which is just the literal value 4, which is within the bounds of the array (index 0 to index 4). We will be changing the value at index 4 within the array. So the last element (or element at index 4) will become the value 22, which is legal as alpha stores only integers and 22 is an integer.
For the second statement, we will be changing the first value (or element at index 0) to 10, which is also an integer and index 0 is within the array bounds.
The array alpha now contains the values:
Figure 11.1.2.

Subsection 11.1.5

alpha[1] = alpha[4] - alpha[0];
For this statement we are changing the value of an array element (SG4) and also accessing array elements (SG3).
For SG4, we will be changing the element at index 1 within the array. We now have to determine the value to be placed at that index, which is the value of the expression on the RHS of the assignment statement. This means we will need to access array elements - so let’s look at SG3.
In looking at the RHS: alpha[4] - alpha[0]
evaluating what is within the [ ] is easy for both accesses, as they are literal values and both values are within array bounds. So we replace the array access with its value from the array:
alpha[4] - alpha[0]
    22 - 10
       12
So the value 12 (an integer) is copied into index 1 of the array.
Figure 11.1.3.

Subsection 11.1.6

alpha[2] = alpha[1] - alpha[0];
For this statement we are changing the value of an array element (SG4) and also accessing array elements (SG3).
For SG4, we will be changing the element at index 2 within the array. We now have to determine the value to be placed at that index, which is the value of the expression on the RHS of the assignment statement. This means we will need to access array elements - so let’s look at SG3.
In looking at the RHS: alpha[1] - alpha[0]
evaluating what is within the [ ] is easy for both accesses, as they are literal values and both values are within array bounds. So we replace the array access with its value from the array:
alpha[1] - alpha[0]
    12 - 10
       2
So the value 2 (an integer) is copied into index 2 of the array.
Figure 11.1.4.

Subsection 11.1.7

alpha[3] = alpha[alpha[2] - 1];
For this statement we are changing the value of an array element (SG4) and also accessing array elements (SG3).
For SG4, we will be changing the element at index 3 within the array (within array bounds). We now have to determine the value to be placed at that index, which is the value of the expression on the RHS of the assignment statement. This means we will need to access array elements - so let’s look at SG3.
In looking at the RHS: alpha[alpha[2]-1];
evaluating what is within the [ ] requires another array access. Notice that we are basically reading this from the inside to the outside. So the first access is alpha[2] (2 is within the bounds of the array) and the value is 12.
alpha[alpha[2] - 1]
    alpha[2 - 1]
       alpha[1]
          12
So the value 12 (an integer) is copied into index 3 of the array.
Figure 11.1.5.

Subsection 11.1.8

alpha[4] = alpha[alpha[3]];
For this statement we are changing the value of an array element (SG4) and also accessing array elements (SG3).
For SG4, we will be changing the element at index 4 within the array (within array bounds). We now have to determine the value to be placed at that index, which is the value of the expression on the RHS of the assignment statement. This means we will need to access array elements - so let’s look at SG3.
In looking at the RHS: alpha[alpha[3]]
evaluating what is within the [ ] requires another array access.
alpha[alpha[3]]
   alpha[12]
12 is not a valid index, it is out of bounds of the array, so an IndexOutOfBounds exception occurs.

Subsection 11.1.9 Practice Pages

You have attempted of activities on this page.