Skip to main content

Section 13.19 Worked Example: ArrayList Parameters

Subgoals for Evaluating ArrayLists.

  1. Declaring and initialization of an ArrayList
    1. Set up a one-dimensional dynamic list (initially empty or with a specified initial capacity)
    2. Upon instantiation, an ArrayList contains zero elements initially, but elements can be added dynamically using add(). Elements not yet added do not exist until explicitly inserted.
  2. Determine access or change of element, or action on entire ArrayList object, and update elements as needed (remembering assignment subgoals)
  3. Accessing an ArrayList element
    1. Evaluate expression within get(index) which will be the index for the element to be accessed
    2. arrayListName.get(index) returns the value stored at that index
    3. index must be between 0 and arrayListName.size() - 1, inclusive; otherwise IndexOutOfBoundsException occurs
  4. Changing value of an ArrayList element
    1. Evaluate expression within set(index, value) which will be the index for the element to be replaced
    2. arrayListName.set(index, value) replaces the element at index with the specified value
    3. (remember the assignment subgoals for verifying data types and evaluating expressions)
    4. (remember rules for index values)
  5. Whole ArrayList actions
    1. Passing as argument - a copy of the reference to the instantiated ArrayList is passed to the method. This means that any changes made to the elements inside the method persist outside the method. The exception is if the argument is assigned to reference a different ArrayList inside the method.
    2. Assignment - changes the reference to point to the ArrayList on the right-hand side of the assignment operator.

Subsection 13.19.1

Problem: Evaluate the following code - what is the output?
import java.util.ArrayList;

public class ArrayListParms {
    public static ArrayList<Integer> arrListMethod (ArrayList<Integer> arrListOne, ArrayList<Person> arrListObj) {
        ArrayList<Integer> temp = new ArrayList<>();
        temp.add(6);
        temp.add(7);
        temp.add(8);
        temp.add(9);
        temp.add(10);
        System.out.println("Inside method arrListMethod:");
        System.out.println("arrListOne is " + printIntArrList(arrListOne));
        System.out.println("temp is " + printIntArrList(temp));
        System.out.println("arrListObj is " + printObjArrList(arrListObj));

        // change values
        for (int i = 0; i < arrListOne.size(); i++) {
            arrListOne.set(i, arrListOne.get(i) + 1);
        }
        arrListObj.get(0).setName("Juan Valdez");
        arrListObj.get(0).setId(362);
        arrListObj.set(2, new Person("Mary Smith", 548));

        System.out.println("At end of method arrListMethod:");
        System.out.println("arrListOne is " + printIntArrList(arrListOne));
        System.out.println("temp is " + printIntArrList(temp));
        System.out.println("arrListObj is " + printObjArrList(arrListObj));
        return temp;
    }

    public static void main(String[] args) {       
        ArrayList<Integer> one = new ArrayList<>();
        one.add(0);
        one.add(1);
        one.add(2);
        one.add(3);
        one.add(4);
        one.add(5);
        ArrayList<Integer> two = new ArrayList<>();
        two.add(0);
        two.add(0);
        ArrayList<Person> gamma = new ArrayList<>();
        gamma.add(new Person("Maria Santos", 156));
        gamma.add(new Person("Caiji Zheng", 742));
        gamma.add(null);
        System.out.println("Before method call:");
        System.out.println("one is " + printIntArrList(one));
        System.out.println("two is " + printIntArrList(two));
        System.out.println("gamma is " + printObjArrList(gamma));
        two = arrListMethod(one, gamma);
        System.out.println("After method call:");
        System.out.println("one is " + printIntArrList(one));
        System.out.println("two is " + printIntArrList(two));
        System.out.println("gamma is " + printObjArrList(gamma));
    }

    public static String printIntArrList(ArrayList<Integer> arrList) {
        String result = "";
        for (Integer o : arrList) {
            result += o;
            result += "  ";
        }
        return result;
    }

    public static String printObjArrList(ArrayList<Person> arrList) {
        String result = "";
        for (Person o : arrList) {
            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 13.19.2 SG1: Declaring and initialization of ArrayList

The first several lines of the main() method are declaring and initializing ArrayLists:
ArrayList<Integer> one = new ArrayList<>();
one.add(0);
one.add(1);
one.add(2);
one.add(3);
one.add(4);
one.add(5);
ArrayList<Integer> two = new ArrayList<>();
two.add(0);
two.add(0);
ArrayList<Person> gamma = new ArrayList<>();
Here is a memory representation:
Figure 13.19.1.

Subsection 13.19.3 SG2: Determine access or action

The next two lines of code are accessing elements of the ArrayList gamma:
gamma.add(new Person("Maria Santos", 156));
gamma.add(new Person("Caiji Zheng", 742));
gamma.add(null);
We are creating and instantiating two new Person instances with the parameters in the overloaded constructor call, and adding them to the ArrayList. Once these statements are completed, here is a memory representation:
Figure 13.19.2.
So these next statements:
System.out.println("Before method call:");
System.out.println("one is " + printIntArrList(one));
System.out.println("two is " + printIntArrList(two));
System.out.println("gamma is " + printObjArrList(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, printIntArrList and printObjArrList that simply concatenate the toString results for each ArrayList element into a string that is returned. These methods are used to print out the contents of the ArrayLists.

Subsection 13.19.4 SG2: Determine access or action

For the next line of code:
two = arrListMethod(one, gamma);
We are passing entire ArrayLists as parameters and assigning the return value of the method to an ArrayList, so we go to SG5. Let’s break this method call down into two separate pieces: passing the parameter and then returning an ArrayList.

Subsection 13.19.5 SG6: Whole ArrayList actions, parameter passing

When we call the method arrListMethod, the value in the parameter one is copied to arrListOne, and the value in the parameter gamma is copied to arrListObj.
The first lines in arrListMethod are just declaring and initializing a new ArrayList.
ArrayList<Integer> temp = new ArrayList<>();
temp.add(6);
temp.add(7);
temp.add(8);
temp.add(9);
temp.add(10);
Here is a memory representation of the parameter passing and new local ArrayList temp:
Figure 13.19.3.
Now we are just printing the values:
System.out.println("Inside method arrListMethod:");
System.out.println("arrListOne is " + printIntArrList(arrListOne));
System.out.println("temp is " + printIntArrList(temp));
System.out.println("arrListObj is " + printObjArrList(arrListObj));
Which generates this output:
Inside method arrListMethod:
arrListOne is 0  1  2  3  4  5  
temp is 6  7  8  9  10  
arrListObj is Person{name='Maria Santos', id=156}  Person{name='Caiji Zheng', id=742}  null

Subsection 13.19.6 Evaluate code

// change values
for (int i = 0; i < arrListOne.size(); i++) {
    arrListOne.set(i, arrListOne.get(i) + 1);
}
arrListObj.get(0).setName("Juan Valdez");
arrListObj.get(0).setId(362);
arrListObj.set(2, new Person("Mary Smith", 548));
Next we have the loop that changes the values in the parameter arrListOne. Notice that arrListOne references the same ArrayList in memory as the argument one in the method call.
We then change values of the second element of the arrListObj ArrayList - which references the same ArrayList in memory as the argument gamma in the method call.
After these changes, our memory representation would be:
Figure 13.19.4.
Before we exit the method, we print the values again:
System.out.println("At end of method arrListMethod:");
System.out.println("arrListOne is " + printIntArrList(arrListOne));
System.out.println("temp is " + printIntArrList(temp));
System.out.println("arrListObj is " + printObjArrList(arrListObj));
Which generates this output:
At end of method arrListMethod:
arrListOne is 1  2  3  4  5  6  
temp is 6  7  8  9  10  
arrListObj 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 13.19.7 SG6: Whole ArrayList actions, ArrayList assignment

Recall the method call statement:
two = arrListMethod(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 ArrayList temp to the variable two.
Here is a memory representation:
Figure 13.19.5.
Afterwards, we print the values using:
System.out.println("After method call:");
System.out.println("one is " + printIntArrList(one));
System.out.println("two is " + printIntArrList(two));
System.out.println("gamma is " + printObjArrList(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 13.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 arrListMethod:
arrListOne is 0  1  2  3  4  5  
temp is 6  7  8  9  10  
arrListObj is Person{name='Maria Santos', id=156}  Person{name='Caiji Zheng', id=742}  null 
At end of method arrListMethod:
arrListOne is 1  2  3  4  5  6  
temp is 6  7  8  9  10  
arrListObj 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 1 of 1 activities on this page.