Skip to main content

Section 12.7 Assessment: Write Arrays

Subgoals for Evaluating Arrays.

  1. Declaring an array variable
    1. Determine data type to be stored
    2. Determine name
    3. Data type followed by [] followed by name
  2. Instantiating an array object
    1. Determine size (number of elements to be stored)
    2. Assign to variable: keyword new, followed by datatype, followed by [size]. (All values initialized to default value for datatype.)
    3. Assign to variable: {initializer list} where initializer list contains the values to be stored in the array
  3. Accessing array element
    1. Determine value of 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 an IndexOutOfBounds exception occurs at runtime
  4. Changing value of an array element
    1. Determine value of index of element to be changed (remember rules for index values)
    2. Determine the expression for RHS (remember the assignment subgoals for verifying data types)
    3. Write assignment statement to update array element
  5. Traversing an array
    1. Decide if updating, accessing all in forward succession, or accessing some subset of array elements or accessing in a different order
    2. If accessing only, write an enhanced for (for each) loop:
      1. for (DataType varName : collectionName) - traverses collectionName from first element to last element storing a copy of each element from collectionName in varName for each iteration of the loop.
    3. If updating or not accessing all elements in forward succession, write a for loop:
      1. start loop control variable (which will also be index) at 0 to go forwards, (arrayName.length - 1) to go backwards
      2. continuation test is loop control variable < arrayName.length for forwards, loop control variable >= 0 for backwards
      3. update is loop control variable increments for forwards, decrements for backwards
    4. inside loop access / update array element using loop control variable as index into array or varName has value of array element
  6. Whole array actions
    1. Passing an array as an argument
      1. Determine that the an entire array must be passed as an argument to a method by consulting documentation
      2. When calling a method, put variable name that represents the array as an argument in the method call. (Remember that when passing an array as an argument that changes made by the method to the array are persistent.)
    2. Array Assignment
      1. Determine that the reference to an array needs to be changed
      2. The LHS of the assignment is the array reference needing to be changed
      3. The RHS of the assignment is the new array reference

Exercises Exercises

    1.

    Q1: The following method is intended to return a String formed by concatenating elements from the parameter words. The elements to be concatenated start with startIndex and continue through the last element of words and should appear in reverse order in the resulting string.
    // Assume that words.length > 0 and startIndex >= 0
    public String concatWords (String [] words, int startIndex) {
       String result = "";
       /* missing code */
       return result;
    }
    
    For example, the execution of the following code segment should result in “CarHouseGorilla” being printed.
    String [] things = {"Bear", "Apple", "Gorilla", "House", "Car"};
    System.out.println(concatWords(things, 2));
    
    Which of the following code segments is a correct replacement for /* missing code */ so that the method will work as intended?
    Figure 12.7.1.

    2.

    Q2: Consider the following two methods that occur in the same class. What is printed as a result to the call start()?
    public void changeIt (int [] list, int num) {
       list = new int[5];
       num = 0;
       for (int x = 0; x < list.length; x++)
          list[x] = 0;
    }
    
    public void start() {
       int [] nums = {1, 2, 3, 4, 5};
       int value = 6;
       changeIt(nums, value);
       for (int k = 0; k < nums.length; k++)
          System.out.print(nums[k] + " ");
       System.out.print(value);
    }
    
    • 0 0 0 0 0 0
    • 0 0 0 0 0 6
    • 1 2 3 4 5 6
    • 1 2 3 4 5 0
    • No output, an exception is thrown

    3.

    Q3: Consider the following two methods that occur in the same class. What is printed as a result to the call start()?
    public void changeAgain (int [] arr, int val, String word) {
       arr = new int[5];
       val = 0;
       word = word.substring(0,5);
       for (int k = 0; k < arr.length; k++)
          arr[k] = 0;
    }
    
    public void start() {
       int [] nums = {1, 2, 3, 4, 5};
       int value = 6;
       String name = "blackboard";
       changeAgain(nums, value, name);
       for (int x = 0; x < nums.length; x++)
          System.out.print(nums[x] + " ");
       System.out.print(value + " ");
       System.out.print(name);
    }
    
    • 0 0 0 0 0 0 black
    • 0 0 0 0 0 6 blackboard
    • 1 2 3 4 5 6 black
    • 1 2 3 4 5 0 black
    • 1 2 3 4 5 6 blackboard
You have attempted 1 of 2 activities on this page.