Skip to main content

Section 13.2 ArrayList-WE1-P1

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

Exercises Exercises

Given
ArrayList<Integer> beta = new ArrayList<Integer>();
for (int i = 0; i < 3; i++)
    beta.add(0);
beta.set(1, 22);
beta.set(0, beta.get(1) - 11);
beta.set(2, beta.get(0) + beta.get(1));
1.
Q1:Give the contents of list beta after the execution of the above code:
beta[0] =
beta[1] =
beta[2] =
Given
ArrayList<String> delta = new ArrayList<String>();
for (int i = 0; i < 4; i++)
    delta.add(" ");
delta.set(0, "apple");
delta.set(1, "Banana");
delta.set(2, delta.get(0).substring(2));
delta.set(3, delta.get(delta.get(1).indexOf('n')));
2.
Q2:Give the contents of list delta after the execution of the above code:
delta[0] =
delta[1] =
delta[2] =
delta[3] =
3.
Q3: If the following lines are compiled (in the order given), which line will generate the first compiler error?
ArrayList<Double> gamma = new ArrayList<Double>();
for (int i = 0; i < 5; i++)
    gamma.add(0.0);
  • gamma.set(0, 14.0);
  • This is a valid statement: sets index 0 to the double value 14.0.
  • gamma.set(1, gamma.get(0));
  • This is also valid: retrieves the double at index 0 and stores it at index 1.
  • gamma.set(gamma.get(0), 42.0);
  • This will not compile because gamma.get(0) returns a Double, but set(index, value) expects an int index. You cannot use a Double as an index.
  • gamma.set(5, 22.0);
  • This will cause a runtime error (IndexOutOfBoundsException), not a compile-time error.
You have attempted 1 of 5 activities on this page.