Skip to main content

Section 11.18 Arrays-WE8-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.18.1

Exercises Exercises

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.
You have attempted of activities on this page.