The terms “ArrayList” and “array” are essentially synonyms. They are the same thing.


ArrayLists are arrays


ArrayLists and arrays are different things

Here is what's right.

Arrays are a special language feature in Java. They have special syntax (e.g., int[] for declaring the type “array of int”, or a[3] for “access element 3 of array a”). ArrayList is a class, like any other class in Java. There is no special syntax for ArrayList.

So, the following code is wrong:

int[][] a = new ArrayList<ArrayList<Integer>>();

You can write (using arrays):

int[][] a = new int[3][2];

Or you can write (using ArrayLists):

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

It is a fact that Java arrays are a built-in language feature, while ArrayList is just some class in the standard library. The ArrayList class is an implementation of the abstract List interface, and it implements that interface by internally using arrays to store the elements of the list.

Where could this misconception come from?

One origin of this misconception may be prior exposure to JavaScript, where new Array(10) would allocate an array, which could then be accessed using the normal array access syntax.

Another possible source of this misconception might be prior knowledge on operator overloading, e.g., if a student already has seen C++.

How can you build on this misconception?

It is not at all unreasonable to assume that in an object-oriented language arrays are classes.

In Java, array types are types. And classes are types. The boundary between types and classes is a bit muddied. E.g., you can get a Class object that describes the type of an array:

int[] a = new int[15];
Class c = a.getClass();

It is also not unreasonable to assume that classes could not only define their own methods, but also overload the language’s operators. This can be done in other object-oriented languages like C++. If a language supports operator overloading, then a library class like ArrayList probably would overload the array-indexing operator array[index] to provide the functionality that Java’s ArrayList class provides in its array.get(index) method.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.