ArrayList Introduction Tutorial

A standard Java single dimensional array has many shortcomings and limitations, the most notable of which is that fact it cannot grow in size. The Arrays class has been around since almost the beginning of Java and contains a zillion overloaded methods that you can perform certain actions on a standard single dimensional array. I would not recommend taking the time to learn the methods of the Arrays class, just know that it is there and it works great with good old regular arrays.

The ArrayList class is my preferred choice over a standard array any day. Just the sheer convenience of being able to add and remove elements from an ArrayList on-the-fly is one of its coolest features. Out of the many methods in the ArrayList class, add(...) and remove(...) are the ones that can easily add and remove individual elements from the ArrayList object. I will be going over all of the methods of the ArrayList class in future tutorials.

There are three basic ways to declare and instantiate an ArrayList object and each of them are really straight forward.
I'll call the first one the "legacy" declaration because this is how it was done before Java supported Generics:
    ArrayList ref = new ArrayList();
    ref.add(new StringBuilder("dan"));
// [Hello, World, 155, 41, dan]
In the above example you can see how easy it is to add new elements to the ArrayList. The ArrayList will automatically grow as you add additional elements, you don't have to worry about the size at all. I'm sure you noticed that when I added elements, I mixed and matched objects throughout the ArrayList. 155 and 41 actually got auto-boxed to Integer objects before they were added to the ArrayList elements. The problem with the legacy declaration is that there is no type-safety and the compiler gives us a warning when we try to compile the above code. Imagine what would happen if we looped through each element of the ArrayList trying to invoke .append("not going to happen") on each element. The only object that supports the .append() method is the element with the StringBuilder object, the other elements would crash out.

I'll call the second one the "generic" declaration because it will support type-safety:
    ArrayList<StringBuilder> ref = new ArrayList<StringBuilder>();
    ref.add(new StringBuilder("Alaska"));
    ref.add(new StringBuilder("Colorado"));
    ref.add(new StringBuilder("Florida"));
    ref.add(new StringBuilder("Hawaii"));
    ref.add(new StringBuilder("New York"));
// [Alaska, Colorado, Florida, Hawaii, New York]
In the above example I used diamond syntax with StringBuilder as the type-variable on the ArrayList declaration side. I also used used diamond syntax with StringBuilder as the type-variable when invoking the ArrayList constructor. This lets the compiler know that I only want StringBuilder objects allowed into my ArrayList object. Any attempt to add a different object type such as ref.add("Utah"); will not compile. I have some nice tutorials on beginner Generics if you are unfamiliar with them: Introduction to Generics Tutorial, Generics: Bounded Type Parameters Tutorial, and Generic Methods Tutorial.

The third way is called type inference for generic instance creation:
    ArrayList<Integer> ref = new ArrayList<>();
This example is practically the same as the "generic" declaration, but notice how the new ArrayList<>() constructor call does not contain a type-variable in the diamond syntax. The type-variable is inferred from the type-variable in the diamond syntax from the declaration (left) side of the statement.

Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>md ArrayListIntro
C:\Java>cd ArrayListIntro

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.

import java.util.*;
class ArrayListIntro {
    public static void main(String args[]) {
        ArrayList ref = new ArrayList();
        ref.add(new StringBuilder("dan"));
        System.out.println("ref = " + ref); 

        ArrayList<StringBuilder> gen = new ArrayList<StringBuilder>();
        gen.add(new StringBuilder("Alaska"));
        gen.add(new StringBuilder("Colorado"));
        gen.add(new StringBuilder("Florida"));
        gen.add(new StringBuilder("Hawaii"));
        gen.add(new StringBuilder("New York"));
        System.out.println("gen = "+ gen);

        ArrayList<Integer> infer = new ArrayList<>();
        infer.add(new Integer(41));
        infer.add(new Integer(82));
        infer.add(54); //autoboxing
        System.out.println("infer = "+ infer);

Now switch back to the command prompt (CMD) and type in javac and press Enter.
Now type in java ArrayListIntro and press Enter.

C:\Java\ArrayListIntro>java ArrayListIntro
ref = [Hello, World, 155, 41, dan]

gen = [Alaska, Colorado, Florida, Hawaii, New York]

infer = [41, 82, 54, 1890, 66]

Final thoughts

Stay tuned to my channel as I will be going over all the ArrayList constructors and methods in future tutorials.