Section 13.17 Worked Example: ArrayList Parameters
Subgoals for Evaluating ArrayLists.
-
Declaration and initialization of an ArrayList
-
Set up a one-dimensional table that will either be empty or have a specified initial capacity based on the parameter to the constructor
-
When declaring an ArrayList, the datatype stored in the container is specified inside of <>, and the data type must be the name of a class (no primitive data types)
-
Upon instantiation, an ArrayList contains zero elements initially, but elements can be added dynamically using
add()
-
-
Determine access or change of an element, or action on entire ArrayList object, and update elements as needed (remembering assignment subgoals)
-
Accessing an ArrayList element
-
Determine the value of the parameter in the
get(<expression>)
method call (remember evaluating expressions subgoals) -
The parameter to the method
get
represents the index in the ArrayList. The size of the ArrayList is the number of elements contained. If the ArrayList is initially empty, the size is 0. -
Index must be between 0 and
arrayListName.size() - 1
, inclusive; otherwiseIndexOutOfBoundsException
occurs -
arrayListName.get(index)
returns the value stored at that index
-
-
Changing value of an ArrayList element
-
Determine the value of the first parameter in the
set(<expression>, value)
method call which will be the index for the element to be updated -
(remember the assignment subgoals for verifying data types and evaluating expressions)
-
(remember rules for index values)
-
-
Whole ArrayList actions
-
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 one exception to this is if you assign the argument to reference a different ArrayList in memory.
-
Assignment - changes the reference to point to the ArrayList on the right-hand side of the assignment operator.
-
Subsection 13.17.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.17.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<>();
gamma.add(null);
gamma.add(null);
gamma.add(null);
Here is a memory representation:

Subsection 13.17.3 SG2: Determine access or action
The next two lines of code are accessing elements of the ArrayList
gamma
:
gamma.set(0, new Person("Maria Santos", 156));
gamma.set(1, new Person("Caiji Zheng", 742));
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:

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.17.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.17.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
:

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.17.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:

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.17.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:

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.17.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 of activities on this page.