What is an ArrayList?

An ArrayList is like an array, except better :)

The main problem with a regular array is that elements can not be added or removed from the array (can not change the array size). This is made possible with ArrayList

To begin, start by creating an ArrayList object:

import java.util.ArrayList;
abstract class ArrayListDemo{
    ArrayList<String> alphabet = new ArrayList<String>(Arrays.asList("a", "b", "c"));
    public ArrayListDemo() {

    }
    protected abstract void changes();

   
}

Add, edit, and remove elements

To add elements, use the add() method. To add an element at a specific index, use add(index, element). To add all elements in an array to another array, use addAll(index, array). To edit elements, use set(index, element). To remove elements at a specific index, use the remove(index) method, or to remove a specific element, use remove(element). To clear an array, use the clear() method.

Add elements

public class SpecificArray extends ArrayListDemo {
    public SpecificArray() {
    }

    public void changes() {
        super.alphabet.add("d");
        super.alphabet.add(1, "z");
        System.out.println(super.alphabet);
    }

    public static void main(String[] args) {
        SpecificArray changeArray = new SpecificArray(); 
        changeArray.changes(); 
    }
}
SpecificArray.main(null);
[a, z, b, c, d]

Edit elements

public class SpecificArray extends ArrayListDemo {
    public SpecificArray() {
    }

    public void changes() {
        super.alphabet.set(0, "z"); 
        System.out.println(super.alphabet);
    }

    public static void main(String[] args) {
        SpecificArray changeArray = new SpecificArray(); 
        changeArray.changes(); 
    }
}
SpecificArray.main(null);
[z, b, c]

Remove elements

public class SpecificArray extends ArrayListDemo {
    public SpecificArray() {
    }

    public void changes() {
        super.alphabet.remove(1);
        super.alphabet.remove("c");
        System.out.println(super.alphabet);
    }

    public static void main(String[] args) {
        SpecificArray changeArray = new SpecificArray(); 
        changeArray.changes(); 
    }
}
SpecificArray.main(null);
[a]

Add an array to another

public class SpecificArray extends ArrayListDemo {
    ArrayList<String> alphabet2 = new ArrayList<String>(Arrays.asList("z", "x", "y")); 

    public SpecificArray() {
    }

    public void changes() {
        super.alphabet.addAll(1, alphabet2); 
        System.out.println(alphabet);     
    }

    public static void main(String[] args) {
        SpecificArray changeArray = new SpecificArray(); 
        changeArray.changes(); 
    }
}
SpecificArray.main(null);
[a, z, x, y, b, c]

Clear

public class SpecificArray extends ArrayListDemo {

    public SpecificArray() {
    }

    public void changes() {
        super.alphabet.clear(); 
        System.out.println(alphabet); 
    }

    public static void main(String[] args) {
        SpecificArray changeArray = new SpecificArray(); 
        changeArray.changes(); 
    }
}
SpecificArray.main(null);
[]

Array Info

To find the size of the array, use the size() method. To see the specific element at an index, use the get() method. indexOf(element) will return the first index that the element is in, while lastIndexOf(element) will return the index of the last occurrence of the element. To find the associated hash of the array, use the hashCode() method. The isEmpty() method will return true if the array is empty, and false if not. To check if the array contains an element, use the contains(element) method. To check if two arrays are equal to each other, use the equals(array) method, and to check if an array contains all of the elements in a collection, use the containsAll(array) method. To sort the elements in an ArrayList, use the sort() method.

ArrayList Info

public class SpecificArray extends ArrayListDemo {

    public SpecificArray() {
    }

    public void changes() {
        System.out.println(super.alphabet); 
        System.out.println("Size of alphabet array is: " + super.alphabet.size()); 
        System.out.println("Element of index 0 is: " + super.alphabet.get(0)); 
        System.out.println("Index of element b is: " + super.alphabet.indexOf("b")); 
        super.alphabet.add("a"); 
        System.out.println("Updated ArrayList: " + super.alphabet); 
        System.out.println("Last occurence of a is at index: " + super.alphabet.lastIndexOf("a")); 
        System.out.println("Hash code of alphabet is " + super.alphabet.hashCode()); 

        if (super.alphabet.isEmpty() == true) {
            System.out.println("alphabet array is empty!");
        } else {
            System.out.println("alphabet array is not empty!"); 
        }

        if (super.alphabet.contains("z") == true) {
            System.out.println("alphabet array contains the element z");
        } else {
            System.out.println("alphabet array does not contain the element z"); 
        }

    }

    public static void main(String[] args) {
        SpecificArray changeArray = new SpecificArray(); 
        changeArray.changes(); 
    }
}
SpecificArray.main(null);
[a, b, c]
Size of alphabet array is: 3
Element of index 0 is: a
Index of element b is: 1
Updated ArrayList: [a, b, c, a]
Last occurence of a is at index: 3
Hash code of alphabet is 3910592
alphabet array is not empty!
alphabet array does not contain the element z
false

Compare ArrayLists

public class SpecificArray extends ArrayListDemo {
    ArrayList<String> alphabet2 = new ArrayList<String>(Arrays.asList("z", "x", "y")); 

    public SpecificArray() {
    }

    public void changes() {
        System.out.println("alphabet: " + alphabet); 
        System.out.println("alphabet2: " + alphabet2); 
        if (alphabet.equals(alphabet2)) {
            System.out.println("alphabet and alphabet2 are the same"); 
        } else {
            System.out.println("alphabet and alphabet2 are different"); 
        }

        if (alphabet.containsAll(alphabet2)) {
            System.out.println("alphabet and alphabet2 have the same elements"); 
        } else {
            System.out.println("alphabet and alphabet2 have different elements"); 
        }
    }

    public static void main(String[] args) {
        SpecificArray changeArray = new SpecificArray(); 
        changeArray.changes(); 
    }
}
SpecificArray.main(null);
alphabet: [a, b, c]
alphabet2: [z, x, y]
alphabet and alphabet2 are different
alphabet and alphabet2 have different elements

Sort ArrayLists

public class SpecificArray extends ArrayListDemo {
    public SpecificArray() {
    }

    public void changes() {
        alphabet.add("a"); 
        System.out.println("Modified alphabet ArrayList: " + alphabet); 

        alphabet.sort(Comparator.naturalOrder()); 
        System.out.println("Sorted ArrayList alphabet: " + alphabet); 
    }

    public static void main(String[] args) {
        SpecificArray changeArray = new SpecificArray(); 
        changeArray.changes(); 
    }
}
SpecificArray.main(null);
Modified alphabet ArrayList: [a, b, c, a]
Sorted ArrayList alphabet: [a, a, b, c]