Learning Objectives

In the context of ArrayList objects, this module aims to teach the following skills:

a. Iterating through ArrayLists using for or while loops.

b. Iterating through ArrayLists using enhanced for loops.

In the realm of algorithms, within the context of specific requirements that demand the utilization of ArrayList traversals, students will be able to:

  • Recognize established algorithms.
  • Customize existing algorithms.
  • Create new algorithms.

Essential Knowledge

  • Iteration statements provide a means to access all the elements stored within an ArrayList. This process is referred to as “traversing the ArrayList.”

  • The following methods related to ArrayLists, their functions, and appropriate use are covered in the Java Quick Reference:

    • int size() - Returns the count of elements within the list.
    • boolean add(E obj) - Appends the object obj to the end of the list and returns true.
    • void add(int index, E obj) - Inserts obj at the specified index, shifting elements at and above that position to the right (incrementing their indices by 1) and increasing the list’s size by 1.
    • E get(int index) - Retrieves the element at the given index in the list.
    • E set(int index, E obj) - Replaces the element at the specified index with obj and returns the previous element at that index.
    • E remove(int index) - Deletes the element at the specified index, shifting all subsequent elements one index to the left, reducing the list’s size by one, and returning the removed element.
  • There exist established algorithms for ArrayLists that make use of traversals to:

    • Insert elements.
    • Remove elements.
    • Apply the same algorithms commonly used with 1D arrays.

Popcorn Hacks:

Before you uncomment the code and run it, guess what the code will do based on what you’ve learned.

Let’s Look at an Example (Example 1)

public class ArrayListExample {
    private double findMax(double[] values) {
        // double max = values[0];
    
        //for (int index = 1; index < values.length; index++) {
        //    if (values[index] > max) {
        //        max = values[index];
        //    }
        //}
    
        // return max;
    }
    
    public static void main(String[] args) {
        double[] nums = {1.0, 3.0, 2.0, 2.0, 1.0, 69.0, 2.0, 4.0, 6.0, 2.0, 5.0, 10.0};
        ArrayListExample example = new ArrayListExample();
        double max = example.findMax(nums);
        System.out.println("Maximum value: " + max);
    }
}

ArrayListExample.main(null);

Take a closer look at the findMax() method. It takes in a list of doubles as parameters. It will then use a for loop to find the maximum value in the list. Now, using what we know, can we replace the list of doubles with an ArrayList of Doubles? We sure can! Take a look at how we can use ArrayList to do just that:

public class ArrayListExample {
    private double findMax(ArrayList<Double> values) {
        // double max = values.get(0);
    
        //for (int index = 1; index < values.size(); index++) {
        //    if (values.get(index) > max) {
        //        max = values.get(index);
        //    }
        //}
    
        //return max;
    }
    
    public static void main(String[] args) {
        ArrayList<Double> nums = new ArrayList<>();
        nums.add(1.0);
        nums.add(3.0);
        nums.add(2.0);
        nums.add(2.0);
        nums.add(1.0);
        nums.add(69.0);
        nums.add(2.0);
        nums.add(4.0);
        nums.add(6.0);
        nums.add(2.0);
        nums.add(5.0);
        nums.add(10.0);
        
        ArrayListExample example = new ArrayListExample();
        double max = example.findMax(nums);
        System.out.println("Maximum value: " + max);
    }
}

ArrayListExample.main(null);

Let’s Look at an Example (Example 2)

Take a look at this code:

public class ArrayListExample {
    private int findMin(int[] values) {
        //int min = Integer.MAX_VALUE;
        //for (int currentValue : values) {
        //    if (currentValue < min) {
        //        min = currentValue;
        //    }
        //}
        return min;
    }

    public static void main(String[] args) {
        int[] nums = {420, 703, 2034, 582, 1047, 4545};
        ArrayListExample example = new ArrayListExample();
        int min = example.findMin(nums);
        System.out.println("Minimum value: " + min);
    }
}

ArrayListExample.main(null);

Now, can we use ArrayLists to make this code better? We sure can! Take a look at the new and improved code that uses ArrayLists:

public class ArrayListExample {
    private int findMin(ArrayList<Integer> values) {
        //int min = Integer.MAX_VALUE;
        //for (int currentValue : values) {
        //    if (currentValue < min) {
        //        min = currentValue;
        //    }
        //}
        return min;
    }

    public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<>();
        nums.add(420);
        nums.add(703);
        nums.add(2034);
        nums.add(582);
        nums.add(1047);
        nums.add(4545);
        ArrayListExample example = new ArrayListExample();
        int min = example.findMin(nums);
        System.out.println("Minimum value: " + min);
    }
}

ArrayListExample.main(null);

Hacks

  • Answer the questions:
    • Look back at the examples. What’s similar? What’s different?
    • Why do we use ArrayList? Why not just regular lists?
  • Demonstrate at least two ArrayList methods that aren’t ArrayList<>.size() and ArrayList<>.get().
  • Write the method findSum() using the Hack Helper and incorporating ArrayList.

Hack Helper

public class ArrayListHacks {
    private int findSum(ArrayList<Integer> values) {
        // Your code here
        return 0;
    }

    public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<>();
        nums.add(0);
        nums.add(1);
        nums.add(2);
        nums.add(3);
        nums.add(5);
        nums.add(8);

        ArrayListHacks hacks = new ArrayListHacks();
        hacks.findSum(nums);
    }
}

ArrayListHacks.main(null);