7.4 Developing Algorithms Using ArrayLists

Common Arraylist Methods:

  • size(): Returns the size of the arraylist as an Integer
  • add(object): Adds an object to the end of your ArrayList
  • void add(index, object): Addes an object to an index of your choice. Shifts the index of everything to the right by one and increases size by 1
  • get(index): Retrieves the object at the index specified
  • set(index, obj): Like void add, but instead of adding, it replaces the object that’s already in that index
  • remove(index): Removes the object at specified index
//size() & add(object)

ArrayList<Double> numbers = new ArrayList<>();
    numbers.add(1.0);
    numbers.add(2.0);
    numbers.add(3.0);

int size = numbers.size();

System.out.println(size);
3
//void add(index, object)
//get(index)
ArrayList<Double> numbers = new ArrayList<>();
    numbers.add(1.0);
    numbers.add(2.0);
    numbers.add(3.0);

System.out.println(numbers.get(2));

    numbers.add(2,4.0);

System.out.println(numbers.get(2));
System.out.println(numbers.get(3));
3.0
4.0
3.0
// set(index, obj)


ArrayList<Double> numbers = new ArrayList<>();
    numbers.add(1.0);
    numbers.add(2.0);
    numbers.add(3.0);

System.out.println(numbers.get(2));

    numbers.set(2,4.0);

System.out.println(numbers.get(2));

3.0
4.0
// remove(index)


ArrayList<Double> numbers = new ArrayList<>();
    numbers.add(1.0);
    numbers.add(2.0);
    numbers.add(3.0);
System.out.println(numbers.get(2));
    numbers.remove(2);

System.out.println(numbers.get(0));
System.out.println(numbers.get(1));
//System.out.println(numbers.get(2));

//anybody know why we get an IndexOutofBoundsException eror?
// cause we removed the index 2, so the size of the array is 2 now, so we can't get the index 2
3.0
1.0
2.0

Here’s an example of a program using Arrays that finds the maximum value:

public class Main {
    public static void main(String[] args) {
        double[] values = {1, 2, 3, 4, 5};
        //valus.add(2, 6);

        double maxValue = findMax(values);
        System.out.println("The maximum value is: " + maxValue);
    }

    private static 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;
    }
}
Main.main(null);
The maximum value is: 5.0

Now, how can we modify this to use an ArrayList?


public class Main {
    public static void main(String[] args) {
        ArrayList<Double> values = new ArrayList<>();
        values.add(1.2);
        values.add(3.4);
        values.add(2.6);
        values.add(4.9);
        values.add(0.8);

        double maxValue = findMax(values);
        System.out.println("The maximum value is: " + maxValue);
    }

    private static 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; 
    }
}
Main.main(null);

The maximum value is: 4.9

Homework:

(Paragraph Answer)

  1. What is the difference between the two examples above. Which one is better and why?

(Code Answer)

  1. Make your own algorithm using ArrayLists that finds the sum of the elements in the ArrayList

The first example uses a fixed-size array, while the second uses an ArrayList, which allows for dynamic resizing and easier manipulation of elements. ArrayList is generally better for flexibility and ease of use, while arrays may be more efficient when the size is known upfront. Overall, the second example is more practical for most use cases due to its versatility.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        // Create an ArrayList and add some elements
        ArrayList<Double> values = new ArrayList<>();
        values.add(1.5);
        values.add(3.8);
        values.add(2.7);
        values.add(4.0);
        values.add(5.3);

        // Find and print the sum of elements
        double sum = findSum(values);
        System.out.println("The sum of the elements is: " + sum);
    }

    // Method to calculate the sum of elements in the ArrayList
    private static double findSum(ArrayList<Double> values) {
        double sum = 0;
        
        // Iterate through the ArrayList and accumulate the sum
        for (int i = 0; i < values.size(); i++) {
            sum += values.get(i);
        }

        return sum;
    }
}

Main.main(null);

The sum of the elements is: 17.3