Skip to main content

Section 13.6 Assessment: ArrayLists 1

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.

Exercises Exercises

    1.

    Q1: Assuming that the following declaration has been made, which of the following code segments correctly interchanges the value of arr.get(0) and arr.get(5)?
    ArrayList<Integer> arr = new ArrayList<>();
    arr.add(5);
    arr.add(12);
    arr.add(-22);
    arr.add(6);
    arr.add(11);
    arr.add(0);
    arr.add(26);
    arr.add(42);
    arr.add(99);
    arr.add(75);
    
    • arr.set(0, 5);
      arr.set(5, 0);
      
    • arr.set(0, arr.get(5));
      arr.set(5, arr.get(0));
      
    • int k = arr.get(5);
      arr.set(0, arr.get(5));
      arr.set(5, k);
      
    • int k = arr.get(0);
      arr.set(0, arr.get(5));
      arr.set(5, k);
      
    • int k = arr.get(5); 
      arr.set(5, arr.get(0)); 
      arr.set(0, arr.get(5));
      

    2.

    Q2: Consider the following code that is intended to print true if all the elements in arr are even numbers; otherwise it should print false. You may assume that arr has been declared and contains valid integer values.
    ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(2, 4, 6, 8, 10));
    boolean isEven = /* expression */;
    for (int k = 0; k < arr.size(); k++) {
       /* loop body */
    }
    if (isEven)
        System.out.println("TRUE");
    else
        System.out.println("FALSE");
    
    Which of the following replacements for /* expression */ and /* loop body */ should be used so that the code works as intended?
    • /* expression */: false /* loop body */: if ((arr.get(k) % 2) == 0) isEven = true;
    • /* expression */: false /* loop body */: if ((arr.get(k) % 2) != 0) isEven = false; else isEven = true;
    • /* expression */: true /* loop body */: if ((arr.get(k) % 2) != 0) isEven = false;
    • /* expression */: true /* loop body */: if ((arr.get(k) % 2) != 0) isEven = false; else isEven = true;
    • /* expression */: true /* loop body */: if ((arr.get(k) % 2) == 0) isEven = false; else isEven = true;

    3.

    Q3: Considering the following code, what are the values in numbers after execution?
    ArrayList<Integer> numbers = new ArrayList<>();
    numbers.add(17);
    numbers.add(34);
    numbers.add(21);
    numbers.add(42);
    numbers.add(15);
    numbers.add(69);
    numbers.add(48);
    numbers.add(25);
    numbers.add(39);
    int x = 3;
    for (int k = 1; k < 9; k = k + x)
       numbers.set(k, numbers.get(k - 1) + x);
    
    • <17, 20, 21, 42, 45, 69, 48, 51, 39>
    • <17, 20, 23, 26, 29, 32, 35, 38, 41>
    • <17, 37, 21, 42, 18, 69, 48, 28, 39>
    • <20, 23, 21, 42, 45, 69, 51, 54, 39>
    • <20, 34, 21, 45, 15, 69, 51, 25, 39>
You have attempted 1 of 2 activities on this page.