Notes

ArrayLists: Dynamic size that allows you to add and remove elements.

ArrayLists are classes while Arrays are objects. This means that ArrayLists have many methods. Arrays also store primitive data while ArrayLists store object references. ArrayLists use wrapper classes instead of primitives.

To use ArrayLists, make sure to import with the import java.util.ArrayList statement.

import java.util.ArrayList;

public class Demo{
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<String>();
        fruits.add("apple");
        fruits.add("pineapple");
        fruits.add("pineapple");
        fruits.add("pears");
        fruits.add("oranges");
        
        for (int i = 0; i < fruits.size(); i++) {
            System.out.println(fruits.get(i)); 
        }
    }
}
Demo.main(null)
apple
pineapple
pineapple
pears
oranges

On the other hand, I can also traverse through ArrayLists using a for each loop.

import java.util.ArrayList;

public class Demo{
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<String>();
        fruits.add("apple");
        fruits.add("pineapple");
        fruits.add("pineapple");
        fruits.add("pears");
        fruits.add("oranges");
        
        for (String fruit : fruits) {
            System.out.println(fruit); 
        }
    }
}
Demo.main(null)
apple
pineapple
pineapple
pears
oranges

Objects

There is a way to compare if two objects have the same attributes using the .equals() method. However, you need to first override the method to do so.

Below is an example:

To override the .equals() method, you need to create the method that takes in an object. In this example, the object is the format of Object obj. In the .equals() method, Object obj refers to test2. Next, obj needs to be typecasted to the class, in this case ObjectTest, for comparison to work. The return statement checks to see if the attributes of the two objects are equal. Note: this refers to test1

public class ObjectTest {
    String attribute1;
    String attribute2; 
    
    // IMPORTANT: forgot to specify String in parameters
    // see https://www.daniweb.com/programming/software-development/threads/346224/identifier-expected-in-constructor-definition
    // youtube reference for comparing objects: https://www.youtube.com/watch?v=X2AjBFZfFCY
    public ObjectTest(String attribute1, String attribute2) {
        this.attribute1 = attribute1; 
        this.attribute2 = attribute2; 
    }

    @Override
    public boolean equals(Object obj) {
        ObjectTest test = (ObjectTest)obj; 
        return this.attribute1.equals(test.attribute1) && this.attribute2.equals(test.attribute2);
    }

    public static void main(String[] args) {
        ObjectTest test1 = new ObjectTest("a", "b"); 
        ObjectTest test2 = new ObjectTest("a", "b"); 

        
        if (test1.equals(test2)) {
            System.out.println("Same attributes"); 
        }
    }
}
ObjectTest.main(null)
Same attributes

Hack 1

// HACK!!!!
// Create an arrayList and use one of the cool methods for it

import java.util.ArrayList; 

public class hack1 {
    public static void main (String[] args) {
       ArrayList<Integer> testArray = new ArrayList<Integer>(); 
       testArray.add(1);
       testArray.add(2);
       System.out.println(testArray.size()); 
    }
}

hack1.main(null);
2

Hack 2

import java.util.ArrayList;

ArrayList<String> color = new ArrayList<String>(); 
color.add("red apple");
color.add("green box");
color.add("blue water");
color.add("red panda");

for (int i = 0; i < color.size(); i++) {
    if (color.get(i).contains("red")) {
        color.remove(i); 
    } 
}

System.out.println(color); 

/*/ 
using 

if(color.get(i).contains("red"))

iterate through the arraylist and remove all elements that contain the word red in them
/*/
[green box, blue water]

Hack 3

// find the sum of the elements in the arraylist

ArrayList<Integer> num = new ArrayList<Integer>(); 

num.add(5);
num.add(1);
num.add(3);

int sum = 0; 

for (int number : num) {
    sum += number;
}
System.out.println(sum);
9

Extra

Below is a binary search algorithm that is focused on unit 7.5 Searching. Binary search works by minimizing the amount of times that the code needs to search through the arraylist in order to find the index where the specific element lies.

For example, in the code below, I created an ArrayList called numberList. The elements in numberList are: [1, 3, 15, 28, 57, 73, 99].

The number that I want to find (secretNum) is 73. Binary searching works by comparing the number in the middle of the ArrayList (28) with the secretNum (73). If the middle number is smaller, binary searching will then search in the middle of the ArrayList from 57 to 99. The middle number is 73, which is at index 5.

The reason why binary searching is more efficient is that if a for loop is used to iterate through each number in the array, then in this example, the numbers in the ArrayList need to be searched 6 times before reaching the secretNum of 73. With binary searching, the algorithm only needs to search 2 times (once in the middle of the ArrayList, the second time in the middle between 57 and 99).

ArrayList<Integer> numbersList = new ArrayList<>(); 

// numbersList: [1, 3, 15, 28, 57, 73, 99]
numbersList.add(1); 
numbersList.add(3); 
numbersList.add(15); 
numbersList.add(28); 
numbersList.add(57); 
numbersList.add(73); 
numbersList.add(99); 

int secretNum = 73; 
int low = 0; 
int high = numbersList.size() - 1;
int index = -1; 
int middle = 0; 
boolean done = false; 

while (!done) {
    int middle = (low + high)/2;
    if (numbersList.get(middle) > secretNum) {
        high = middle - 1; 
    } else if (numbersList.get(middle) < secretNum) {
        low = middle + 1; 
    } else if (numbersList.get(middle) == secretNum) {
        index = middle; 
        done = true; 
    }
}

System.out.println(secretNum + " is located at index: " + index);
73 is located at index: 5

Quizizz


Reflection:

The question that I got wrong on my first attempt was:

You must type _____ at the top of your java file in order to use an ArrayList. (Hint: it is a imported package)

I originally thought the answer was import java.util.Arrays;, but this is for arrays, not arraylists. The correct answer is import java.util.ArrayList;.