Skip to main content

Section 13.18 ArrayLists-WE8-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.18.1

Exercises Exercises

1.
Q24: What does the following code accomplish? Assume that alpha is a correctly declared ArrayList with non-default values and that the variable target contains a value.
int place = -1;
int index = 0;
for (int x : alpha) {
    if (x == target)
        place = index;
    index += 1;
}
  • place contains the index of the first occurrence of target within alpha, -1 otherwise
  • place contains the index of the last occurrence of target within alpha, -1 otherwise
  • place always contains -1
  • the code "finds" target within alpha
  • the code does not work because you can’t get the index with an enhanced for loop
2.
Q25: What is the output of the following code?
ArrayList<Integer> beta = new ArrayList<>(Arrays.asList(5, 10, 15, 20, 25, 30, 35, 40));
for (int x : beta) {
    x += 1;
}
for (int x : beta) {
    System.out.print(x + "  ");
}
  • 5 10 15 20 25 30 35 40
  • 6 11 16 21 26 31 36 41
  • 1 2 3 4 5 6 7 8
  • 6 10 15 20 25 30 35 40
  • no output, the code does not work
3.
Q26: What does the following code accomplish? Assume that alpha is a correctly declared ArrayList with non-default values and that the variable target contains a value.
int max = -1000;
for (int x : alpha) {
     if (x > max)
        max = x;
}
  • max contains the index of the maximum value within alpha (provided the max is > -1000)
  • max contains the index of the maximum value within alpha
  • max contains the maximum value within alpha
  • max contains the maximum value within alpha (provided the max is > -1000)
  • The code does not work
4.
Q27: Put the following code in order to create a method that will return the average of the values in the ArrayList parameter.
You have attempted 1 of 4 activities on this page.