ArrayList Constructors Tutorial

The ArrayList class has three overloaded constructors. The first constructor has no arguments and creates an ArrayList with an initial capacity of ten object elements.
ArrayList<String> a = new ArrayList<String>();
Interestingly, there is no method or property to get the current capacity of your ArrayList object. Other classes such as StringBuilder have a method .capactiy() that returns the current instance capacity, ArrayList does not.

The next constructer allows you to set the initial capacity to a positive integer value.
ArrayList<String> a = new ArrayList<String>(41); // capacity is 41 object elements
Just to reiterate, there is no method or property to get the current capacity of your ArrayList object.

The last constructer allows you to pass in an Collection object - usually an array of some sort. The ArrayList class is a small piece of what is known as the Collections Framework. The Collections Framework is a massive topic ... I suppose that Error Handling is similar in scope. By now you should be familiar with NullPointerException and just like it is a single class in the topic of Exception Handling, ArrayList is a single class in the Collections Framework. One of the most important things to keep in mind is that the ArrayList class implements an interface called List. Don't worry about the collections framework just yet, you will understand more about it is as you progress along. In the example below I will demonstrate how to convert and pass an ordinary array as an argument to the ArrayList constructor.

String sArray[] = { "Alaska", "Colorado", "Kentucky", "Ohio", "Texas", "Vermont" }; // ordinary good old array
List<String> list = Arrays.asList(sArray);
ArrayList<String> c = new ArrayList<String>(list);
// [Alaska, Colorado, Kentucky, Ohio, Texas, Vermont]

In my ArrayList Introduction Tutorial I discussed the three basic ways to declare and initialize an ArrayList instance. Throughtout my tutorials I will NOT be demonstrating the legacy syntax, ArrayList a = new ArrayList(). I will be demonstrating only the generic type-safe versions, ArrayList<Type-Variable> a = new ArrayList<TypeVariable>(); and ArrayList<Type-Variable> a = new ArrayList<>();

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 ArrayListConstructors
C:\Java>cd ArrayListConstructors

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 ArrayListConstructors {
    public static void main(String args[]) {
        ArrayList<String> a = new ArrayList<String>();
        int counter = 0;
        for(String s : a ) {
        System.out.println("No constructor arguments capacity: (can't obtain) " + counter);

        ArrayList<String> b = new ArrayList<String>(41);
        counter = 0;
        for(String s : b ) {
        System.out.println("Constructor argument 41 capacity: (can't obtain) " + counter);

        String sArray[] = { "Alaska", "Colorado", "Kentucky", "Ohio", "Texas", "Vermont" };
        List<String> list = Arrays.asList(sArray);
        ArrayList<String> c = new ArrayList<String>(list);
        for (String s: c) {
            System.out.println("ArrayList c element: " + s);

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

C:\Java\ArrayListConstructors>java ArrayListConstructors
No constructor arguments capacity: (can't obtain) 0
Constructor argument 41 capacity: (can't obtain) 41
ArrayList c element: Alaska
ArrayList c element: Colorado
ArrayList c element: Kentucky
ArrayList c element: Ohio
ArrayList c element: Texas
ArrayList c element: Vermont
ArrayList c element: Hawaii
[Alaska, Colorado, Kentucky, Ohio, Texas, Vermont, Hawaii]

Final thoughts

I have no idea why there is no method or property for obtaining the capacity of the current ArrayList object. That doesn't make sense to me considering there are two constructors and two methods that affect the ArrayList capacity. Don't worry about fully understanding the List interface and the Arrays.asList() method, I will be explaining more about them in future tutorials.