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?
3.0
1.0
2.0



---------------------------------------------------------------------------

java.lang.IndexOutOfBoundsException: Index 2 out of bounds for length 2

	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)

	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)

	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)

	at java.base/java.util.Objects.checkIndex(Objects.java:361)

	at java.base/java.util.ArrayList.get(ArrayList.java:427)

	at .(#38:1)

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};

        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? Array, because the example does not involve dynamically editing the array/arraylist. The array also uses less memory than the arraylist.

(Code Answer)

  1. Make your own algorithm using ArrayLists that finds the sum of the elements in the ArrayList
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 ct = 0;

for (int i = 0; i < values.size(); i++) {
    ct += values.get(i);
}

System.out.println(ct);
12.9