Skip to main content

Section 13.13 Assessment: ArrayLists 2

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: The following code is intended to store the sum of all the values in the integer ArrayList list in the variable total. Which of the following code segments can be used to replace /* missing code */ so that the code works as intended?
    int total = 0;
    /* missing code */
    System.out.println(total);
    
    Options:
    I.  for (int pos = 0; pos < list.size(); pos++)
            total += list.get(pos);
    
    II. for (int pos = list.size(); pos > 0; pos--)
            total += list.get(pos);
    
    III. int pos = 0;
         while (pos < list.size()) {
             total += list.get(pos);
             pos++;
         }
    

    2.

    Q2: Assuming that nums has been declared and initialized as an ArrayList of Integer values, which of the following best describes what this code does?
    int index = 0;
    int count = 0;
    int m = -1;
    for (int outer = 0; outer < nums.size(); outer++) {
       count = 0;
       for (int inner = outer + 1; inner < nums.size(); inner++) {
          if (nums.get(outer).equals(nums.get(inner)))
             count++;
       }
       if (count > m) {
          index = outer;
          m = count;
       }
    } // end outer for
    System.out.println(index);
    
    • Prints the maximum value that occurs in the ArrayList nums
    • Prints the index of the maximum value that occurs in the ArrayList nums
    • Prints the number of times that the maximum value occurs in the ArrayList nums
    • Prints the value that occurs most often in the ArrayList nums
    • Prints the first index of the value that occurs the most often in the ArrayList nums

    3.

    Q3: The following code is intended to store the largest value in the integer ArrayList list in the variable maxVal. Which of the following best describes the conditions under which the code will not work as intended?
    int maxVal = 0;
    for (int val : list) {
       if (val > maxVal)
          maxVal = val;
    }
    
    • The largest value in list occurs only once and is at index 0.
    • The largest value in list occurs only once and is at the last index.
    • The largest value in list is negative.
    • The largest value in list is 0.
    • The largest value in list occurs more than once.

    4.

    Q4: The following code segments are supposed to find the maximum value in an ArrayList of integers. Assuming that the ArrayList list has been declared and contains valid integer values, which of the following code segments will correctly assign the maximum value in the ArrayList to the variable max?
    Options:
    I.  int max = Integer.MIN_VALUE;
        for (int value : list) {
            if (max < value)
                max = value;
        }
    
    II. int max = 0;
        boolean first = true;
        for (int value : list) {
            if (first) {
                max = value;
                first = false;
            }
            else if (max < value)
                max = value;
        }
    
    III. int max = list.get(0);
         for (int k = 1; k < list.size(); k++) {
             if (max < list.get(k))
                 max = list.get(k);
         }
    

    5.

    Q5: The following method is intended to return the index of the first occurrence of the value val beyond the position start in the ArrayList list.
    // returns index of first occurrence of val in list after
    // position start;
    // returns list.size() if val is not found
    public int findNext (ArrayList<Integer> list, int val, int start) {
       int pos = start + 1;
       while ( /* condition */)
          pos++;
       return pos;
    }
    
    For example, the execution of the following code segment should result in the value 4 being printed:
    ArrayList<Integer> list = new ArrayList<>();
    list.add(11); list.add(22); list.add(100); list.add(33); 
    list.add(100); list.add(11); list.add(44); list.add(100);
    System.out.println(findNext(list, 100, 2));
    
    Which of the following expressions could be used to replace /* condition */ so that findNext will work as intended?
    • (pos < list.size()) && (list.get(pos) != val)
    • (list.get(pos) != val) && (pos < list.size())
    • (pos < list.size()) || (list.get(pos) != val)
    • (list.get(pos) == val) && (pos < list.size())
    • (pos < list.size()) || (list.get(pos) == val)
You have attempted 1 of 2 activities on this page.