Skip to main content

Section 13.2 ArrayList-WE1-P1

Subgoals for Evaluating ArrayLists.

  1. Declaring and initialization of an ArrayList
    1. Set up a one-dimensional dynamic list (initially empty or with a specified initial capacity)
    2. Upon instantiation, an ArrayList contains zero elements initially, but elements can be added dynamically using add(). Elements not yet added do not exist until explicitly inserted.
  2. Determine access or change of element, or action on entire ArrayList object, and update elements as needed (remembering assignment subgoals)
  3. Accessing an ArrayList element
    1. Evaluate expression within get(index) which will be the index for the element to be accessed
    2. arrayListName.get(index) returns the value stored at that index
    3. index must be between 0 and arrayListName.size() - 1, inclusive; otherwise IndexOutOfBoundsException occurs
  4. Changing value of an ArrayList element
    1. Evaluate expression within set(index, value) which will be the index for the element to be replaced
    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 exception is if the argument is assigned to reference a different ArrayList inside the method.
    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

1.
Q1: Assume the following declarations:
ArrayList<Integer> beta = new ArrayList<Integer>(Arrays.asList(0, 0, 0));
Evaluate these statements:
beta.set(1, 22);
beta.set(0, beta.get(1) - 11);
beta.set(2, beta.get(0) + beta.get(1));
Give the contents of list beta after the execution of the above statements:
beta[0] =
beta[1] =
beta[2] =
2.
Q2: Assume the following declarations:
ArrayList<String> alpha = new ArrayList<String>(Arrays.asList(null, null, null, null));
Evaluate these statements:
alpha.set(0, "apple");
alpha.set(1, "Banana");
alpha.set(2, alpha.get(0).substring(2));
alpha.set(3, alpha.get(alpha.get(1).indexOf('n')));
Give the contents of list alpha after the execution of the above statements:
alpha[0] =
alpha[1] =
alpha[2] =
alpha[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>(Arrays.asList(0.0, 0.0, 0.0, 0.0, 0.0));
  • This is a correct declaration and initialization of a list of 5 doubles.
  • 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.