Skip to main content

Section 13.11 Worked Example: ArrayList Parameters

Subgoals for Evaluating ArrayLists.

  1. Declaration and initialization of an ArrayList
    1. Set up a one-dimensional table that will either be empty or have a specified initial capacity based on the parameter to the constructor
    2. 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)
    3. Upon instantiation, an ArrayList contains zero elements initially, but elements can be added dynamically using add()
  2. Determine access or change of an element, or action on entire ArrayList object, and update elements as needed (remembering assignment subgoals)
  3. Accessing an ArrayList element
    1. Determine the value of the parameter in the get(<expression>) method call (remember evaluating expressions subgoals)
    2. 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.
    3. Index must be between 0 and arrayListName.size() - 1, inclusive; otherwise IndexOutOfBoundsException occurs
    4. arrayListName.get(index) returns the value stored at that index
  4. Changing value of an ArrayList element
    1. 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
    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 one exception to this is if you assign the argument to reference a different ArrayList in memory.
    2. 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:
Figure 13.11.1.

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:
Figure 13.11.2.

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:
Figure 13.11.3.
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:
Figure 13.11.4.
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));
}
Figure 13.11.5.
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.