Skip to main content

Section 11.20 Arrays-WE9-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.20.1

This example uses the Person class as defined in 11.19.
For the following code, choose the appropriate output produced by each statement:
public class ArrayParms {
    public static int[] arrMethod (int[] arrOne, Person[] arrObj) {
        int [] temp = {6, 7, 8, 9, 10};
        System.out.println("Inside method arrMethod:");
        System.out.println("arrOne is " + printIntArr(arrOne));
        System.out.println("temp is " + printIntArr(temp));
        System.out.println("arrObj is " + printObjArr(arrObj));

        // change values
        for (int i = 0; i < arrOne.length; i++) {
            arrOne[i] += 1;
        }
        arrObj[0].setName("Juan Valdez");
        arrObj[0].setId(362);
        arrObj[2] = new Person("Mary Smith", 548);

        System.out.println("At end of method arrMethod:");
        System.out.println("arrOne is " + printIntArr(arrOne));
        System.out.println("temp is " + printIntArr(temp));
        System.out.println("arrObj is " + printObjArr(arrObj));
        return temp;
    }

    public static void main(String[] args) {       
        int [] one = {0, 1, 2, 3, 4, 5};
        int [] two = new int[2];
        Person [] gamma = new Person[3];
        gamma[0] = new Person("Maria Santos", 156);
        gamma[1] = new Person("Caiji Zheng", 742);
        System.out.println("Before method call:");
        System.out.println("one is " + printIntArr(one));
        System.out.println("two is " + printIntArr(two));
        System.out.println("gamma is " + printObjArr(gamma));
        two = arrMethod(one, gamma);
        System.out.println("After method call:");
        System.out.println("one is " + printIntArr(one));
        System.out.println("two is " + printIntArr(two));
        System.out.println("gamma is " + printObjArr(gamma));
    }

    public static String printIntArr(int[] arr) {
        String result = "";
        for (int o : arr) {
            result += o;
            result += "  ";
        }
        return result;
    }

    public static String printObjArr(Object[] arr) {
        String result = "";
        for (Object o : arr) {
            result += o;
            result += "  ";
        }
        return result;
    }
}

Exercises Exercises

1.
Q30: Select the correct output produced by this code:
System.out.print("Before method call:");
System.out.print(" one is " + printIntArr(one));
System.out.print(" two is " + printIntArr(two));
System.out.print(" gamma is " + printObjArr(gamma));
Option A:
Before method call:
one is 0  1  2  3  4  5 
two is 0  1 
gamma is Person{name='Maria Santos', id=156}  Person{name='Caiji Zheng', id=742}  null
Option B:
Before method call:
one is 0  1  2  3  4  5 
two is 0  0 
gamma is Person{name='Maria Santos', id=156}  Person{name='Caiji Zheng', id=742}  null
Option C:
Before method call: 
one is 0  1  2  3  4  5 
two is 0  0 
gamma is Person{name='Maria Santos', id=156}  Person{name='Caiji Zheng', id=742}
Option D:
Before method call: 
one is 0  1  2  3  4  5 
two is 0  1 
gamma is Person{name='Maria Santos', id=156}  Person{name='Caiji Zheng', id=742}
2.
Q31: Select the correct output produced by this code:
two = arrMethod(one, gamma);
Option A:
Inside method arrMethod: 
arrOne is 0  1  2  3  4  5  
temp is 6  7  8  9  10  
arrObj is Person{name='Maria Santos', id=156}  Person{name='Caiji Zheng', id=742}  null  
At end of method arrMethod: 
arrOne is 0  1  2  3  4  5  
temp is 6  7  8  9  10  
arrObj is Person{name='Juan Valdez', id=362}  Person{name='Caiji Zheng', id=742}  Person{name='Mary Smith', id=548}
Option B:
Inside method arrMethod: 
arrOne is 0  1  2  3  4  5  
temp is 6  7  8  9  10  
arrObj is Person{name='Maria Santos', id=156}  Person{name='Caiji Zheng', id=742}  null  
At end of method arrMethod: 
arrOne is 1  2  3  4  5  6  
temp is 6  7  8  9  10  
arrObj is Person{name='Maria Santos', id=156}  Person{name='Caiji Zheng', id=742}  Person{name='Mary Smith', id=548}
Option C:
Inside method arrMethod: 
arrOne is 0  1  2  3  4  5  
temp is 6  7  8  9  10  
arrObj is Person{name='Maria Santos', id=156}  Person{name='Caiji Zheng', id=742}  null  
At end of method arrMethod: 
arrOne is 1  2  3  4  5  6  
temp is 6  7  8  9  10  
arrObj is Person{name='Juan Valdez', id=362}  Person{name='Caiji Zheng', id=742}  Person{name='Mary Smith', id=548}
Option D:
Inside method arrMethod: 
arrOne is 0  1  2  3  4  5  
temp is 6  7  8  9  10  
arrObj is Person{name='Maria Santos', id=156}  Person{name='Caiji Zheng', id=742}  null  
At end of method arrMethod: 
arrOne is 1  2  3  4  5  6  
temp is 6  7  8  9  10  
arrObj is Person{name='Juan Valdez', id=362}  Person{name='Caiji Zheng', id=742}  null
3.
Q32: Select the correct output produced by this code:
System.out.println("After method call:");
System.out.println("one is " + printIntArr(one));
System.out.println("two is " + printIntArr(two));
System.out.println("gamma is " + printObjArr(gamma));
Option A:
After method call: 
one is 1  2  3  4  5  6  
two is 7  8  9  10  
gamma is Person{name='Juan Valdez', id=362}  Person{name='Caiji Zheng', id=742}  Person{name='Mary Smith', id=548}
Option B:
After method call: 
one is 1  2  3  4  5  6  
two is 6  7  8  9  10  
gamma is Person{name='Maria Santos', id=156}  Person{name='Caiji Zheng', id=742}  Person{name='Mary Smith', id=548}
Option C:
After method call: 
one is 1  2  3  4  5  6  
two is 0  0  
gamma is Person{name='Juan Valdez', id=362}  Person{name='Caiji Zheng', id=742}  Person{name='Mary Smith', id=548}
Option D:
After method call: 
one is 1  2  3  4  5  6  
two is 6  7  8  9  10  
gamma is Person{name='Juan Valdez', id=362}  Person{name='Caiji Zheng', id=742}  Person{name='Mary Smith', id=548}
You have attempted of activities on this page.