Section 13.11 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
getrepresents 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; otherwiseIndexOutOfBoundsExceptionoccurs -
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.11.1
Problem: Evaluate the following code - what is the output?
import java.util.ArrayList;
public class ArrayListParams {
public static void main(String[] args) {
ArrayList<Person> people = new ArrayList<Person>();
people.add(new Person("Maria Santos", 156));
people.add(new Person("Caiji Zheng", 742));
manipulatePeople(people);
System.out.println(people);
otherPeople(people);
System.out.println(people);
}
public static void manipulatePeople(ArrayList<Person> alpha) {
if (alpha.size() > 0) {
alpha.set(0, new Person("Juan Valdez", 360));
}
}
public static void otherPeople(ArrayList<Person> beta) {
beta = new ArrayList<Person>();
beta.add(new Person("Goofy", 111));
beta.add(new Person("Donald", 222));
}
} // end class
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.11.2 SG1: Declaration and initialization of ArrayList
The first 3 lines of the
main() method are declaring an ArrayList and adding two values (both of which are new Person instances):
ArrayList<Person> people = new ArrayList<Person>();
people.add(new Person("Maria Santos", 156));
people.add(new Person("Caiji Zheng", 742));
Here is a memory representation:

Subsection 13.11.3 SG2: Determine access or action
For the next line of code:
manipulatePeople(people);
We are passing the entire ArrayList as a parameter to a method, so we go to SG5A.
Subsection 13.11.4 SG5: Whole ArrayList actions, parameter passing
When we call the method
manipulatePeople, the reference of the ArrayList people is copied into the parameter alpha.
public static void manipulatePeople(ArrayList<Person> alpha) {
Here is a memory representation of the parameter passing:

Subsection 13.11.5 Evaluate code
if (alpha.size() > 0) {
alpha.set(0, new Person("Juan Valdez", 360));
Here the code is determining if the ArrayList parameter is empty. If it is not empty, then it is changing what the first element of the ArrayList references. Now the memory representation would look like this:

We return to the main method and we print the values in the ArrayList:
[Person{name=βJuan Valdezβ, id=360}, Person{name=βCaiji Zhengβ, id=742}]
Subsection 13.11.6 Evaluate code
Continuing on with the logic in the main method, the next line is a method call to the otherPeople method. This method call behaves in the same manner as the previous method call.
otherPeople(people);
Here is a memory representation of the parameter passing:

Within the otherPeople method, we point the beta reference to a new ArrayList and add values to that new ArrayList:
public static void otherPeople(ArrayList<Person> beta) {
beta = new ArrayList<Person>();
beta.add(new Person("Goofy", 111));
beta.add(new Person("Donald", 222));
}

Notice that the original ArrayList is unchanged, so that when we return to the main method and print the values, we get the same values as before:
[Person{name=βJuan Valdezβ, id=360}, Person{name=βCaiji Zhengβ, id=742}]
Subsection 13.11.7 Final Output
The final output of the program is:
Answer.
[Person{name='Juan Valdez', id=360}, Person{name='Caiji Zheng', id=742}]
[Person{name='Juan Valdez', id=360}, Person{name='Caiji Zheng', id=742}]
You have attempted of activities on this page.
