Skip to main content

Section 11.13 Worked Example: Arrays - Minimum Value

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

Problem: Assume that the integer array alpha has been properly declared and initialized with non-default values. What does this code accomplish?
int min = alpha[0];
for (int i = 1; i < alpha.length; i++) {
    if (alpha[i] < min)
        min = alpha[i];
}

Subsection 11.13.2 SG1: Declaring and initialization of array

There is no explicit declaration or initialization of an array within the code. However, within the code there are [ ] (three different places!), so we know we are accessing an array.
Figure 11.13.1.
  • alpha is an array of ints and has values, but we don’t know what those values are
  • however, we can still diagram a representation of this array
  • notice that the largest index is size - 1

Subsection 11.13.3 SG2: Determine access or action

Within the loop, we are accessing array elements.

Subsection 11.13.4 Evaluating code

int min = alpha[0];
The first statement is valid because alpha stores integers, and 0 is a valid index. You can assign an int value to an int variable such as min.This statement is storing the value of the first element in the array in the variable min.
In Java, arrays have a final attribute called length which stores the size of the array. To access the size of the array you use array_name.length.
for (int i = 1; i < alpha.length; i++) {
   if (alpha[i] < min)
      min = alpha[i];
}
  • This loop has index i go from 0 to size - 1 (<length) by increments of 1.
  • Then the value at alpha[i] is compared to min. If the value at alpha[i] is less than min, then alpha[i] is copied into min.
  • All indexes into the array are valid, and all assignments are valid.
Let us trace with a sample array.
Figure 11.13.2.
The first line of the code sample initializes min to copy the value from alpha[0], which in our sample is 12, and then a for-loop is used to traverse the array. The chart below uses one line to represent the memory and calculations during each iteration of the loop, starting when i has a value of zero.
Figure 11.13.3.
We can see that each time a smaller value is located in the array, that value is stored in min.
What does this code accomplish?
Answer.
min contains the smallest value found in the array alpha.

Subsection 11.13.5 Practice Pages

You have attempted of activities on this page.