Section 11.19 Worked Example: Array Parameters
Subgoals for Evaluating Arrays.
-
Declaring and initialization of array
- Set up a one dimensional table (i.e., one row) with 0 to (size - 1) elements
- Upon instantiation of an array object, all elements contain default value for datatype stored in array OR values from the initializer list
- Determine access or change of element, or action on entire array object, and update slots as needed (remembering assignment subgoals)
-
Accessing array element
- Evaluate expression within [] which will be the index for element to be accessed
arrayName[index]
returns value stored at that index- index must be between 0 and
arrayName.length
- 1, inclusive otherwiseIndexOutOfBounds
exception occurs
-
Changing value of an array element
- Evaluate expression within [] which will be the index for element to be accessed
arrayName[index]
will now contain the value on the RHS of assignment statement- (remember the assignment subgoals for verifying data types and evaluating expressions)
- (remember rules for index values)
-
Whole array actions
- 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.
- Assignment - changes the reference to point to the array on the RHS of the assignment operator.
Subsection 11.19.1
Problem: Evaluate the following code - what is the output?
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;
}
}
This code also makes use of a
Person
class, which is defined here:public class Person {
private String name; // name of person
private int id; // person's id
// overloaded constructor
public Person(String name, int id) {
setName(name);
setId(id);
}
// default constructor
public Person() {
}
// Accessors and Mutators
public String getName() {
return name;
}
public void setName(String name) {
if (name.length() != 0) // name must not be null
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
// toString to allow conversion to String
public String toString() {
return "Person{" + "name='" + name + '\'' + ", id=" + id + '}';
}
}
Subsection 11.19.2 SG1: Declaring and initialization of array
The first 3 lines of the
main()
method are declaring and initializing arrays:int [] one = {0, 1, 2, 3, 4, 5};
int [] two = new int[2];
Person [] gamma = new Person[3];
Here is a memory representation:
Subsection 11.19.3 SG2: Determine access or action
The next two lines of code are accessing elements of the array
gamma
:gamma[0] = new Person("Maria Santos", 156);
gamma[1] = new Person("Caiji Zheng", 742);
We are creating and instantiating two new
Person
instances with the parameters in the overloaded constructor call. Once these statements are completed, here is a memory representation:So these next statements:
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));
Will generate this output:
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
Notice there are two helper methods,
printIntArr
and printObjArr
that simply concatenate the toString
results for each array element into a string that is returned. These methods are used to print out the contents of the arrays.Subsection 11.19.4 SG2: Determine access or action
For the next line of code:
two = arrMethod(one, gamma);
We are passing entire arrays as parameters and assigning the return value of the method to an array, so we go to SG5. Let’s break this method call down into two separate pieces: passing the parameter and then returning an array.
Subsection 11.19.5 SG6: Whole array actions, parameter passing
When we call the method
arrMethod
, the value in the parameter one
is copied to arrOne
, and the value in the parameter gamma
is copied to arrObj
.The first line in
arrMethod
is just declaring and initializing a new array.int [] temp = {6, 7, 8, 9, 10};
Here is a memory representation of the parameter passing and new local array
temp
:Now we are just printing the values:
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));
Which generates this output:
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
Subsection 11.19.6 Evaluate code
// 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);
Next we have the loop that changes the values in the parameter
arrOne
. Notice that arrOne
references the same array in memory as the argument one in the method call.We then change values of the second element of the
arrObj
array - which references the same array in memory as the argument gamma
in the method call.After these changes, our memory representation would be:
Before we exit the method, we print the values again:
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));
Which generates this output:
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}
The last line of the method:
return temp;
brings us to our next subgoal.
Subsection 11.19.7 SG6: Whole array actions, array assignment
Recall the method call statement:
two = arrMethod(one, gamma);
which will take the returned value from the method and assign it to the left hand side variable. Notice that this has the effect of assigning the reference to array
temp
to the variable two
.Here is a memory representation:
Afterwards, we print the values using:
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));
Which generates this output:
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}
Subsection 11.19.8
Answer.
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
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}
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.