Skip to main content

Section 13.5 ArrayList-WE2-P2

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.5.1

Assume the following given declarations:
ArrayList<Integer> beta;
Give the contents of beta after the execution of these statements.
int size = 4;
beta = new ArrayList<Integer>();
beta.add(50);
for (int i = 1; i < size; i++)
    beta.add(beta.get(i-1) - 2);

Exercises Exercises

1.
Q8: The value of beta.get(0) is .
2.
Q9: The value of beta.get(1) is .
3.
Q10: The value of beta.get(2) is .
4.
Q11: The value of beta.get(3) is .
You have attempted 1 of 6 activities on this page.