Array vs ArrayList in Java


The difference between Arrays and ArrayList is one of the common questions beginners come across the path of Arrays. Both Array and ArrayList is used to store elements. The elements stored can be of primitive type or objects in the case of Arrays, and only of object type in the case of ArrayList.

Difference between Array and ArrayList

The major difference between Arrays and ArrayList is the static nature of Array, and dynamic nature of ArrayList. Once created you cannot alter the size of an Array, whereas an ArrayList can re-size itself as and when required.
Another important difference is that, an Array is a basic functionality provided by Java. On the other side, ArrayList is a part of collection framework in Java. We can access array members using the square bracket, in which we can specify the index. While, to access ArrayList members and modify them, there are a set of methods.
Let us first see how Arrays and ArrayLists are created.


Array
int arr[] = new int[5];
ArrayList
ArrayList arrL = new ArrayList();

Now, let us look at an example code to better understand the difference between Arrays and ArrayLists.

Output

Let us look at the output of the above example code.

Array vs ArrayList
Fig.1- Array vs ArrayList.

The code is pretty simple and self explanatory. However, there is one point to be noticed here. Let us look at line number 15. We have declared the type as “Integer” instead of “int,” what will happen if we declare the type as “int” instead of “Integer.” Let us have a look.

Output

Let us look at the output of the above example code.

ArrayList Primitive Type Error
Fig.2- ArrayList Primitive Type Error.

The error simply indicates that the type argument cannot be of primitive type.
This is what we mean, when we say, Arrays can contain both primitive data type as well as objects of a class as per the definition of the Array. However, ArrayLists only supports object entries and not primitive data types.

Dynamic Nature of ArrayList

Dynamic Nature of ArrayList
Fig.3- Dynamic Nature of ArrayList.

As you can see in the above given code, the intialCapacity of the ArrayList is set to 5, but we have added elements beyond its capacity. That is exactly what I was talking about, the dynamic nature of ArrayList.
You can create an ArrayList without mentioning its size too. But even if you do, you can add elements to the ArrayList beyond its size.


Difference between length of an Array and size of an ArrayList

Let us look at an example on how to calculate the size of an Array or ArrayList.

Size of Array and ArrayList
Fig.4- Size of Array and ArrayList

Array has length property which provides the length of the Array or Array object. It is the total space allocated in memory during the initialization of the array. We use the length() method to do that.
In the case of ArrayList, it does not have the length property. The java ArrayList has size() method for ArrayList which provides the total number of objects available in the collection.
There are many more methods which can be used to perform certain specific tasks in an ArrayList.

Summary

We can summarize the whole article with the points given below.

  • Arrays are static in nature, whereas, ArrayLists are dynamic in nature.
  • Arrays can hold both primitive data type as well as objects, whereas, ArrayLists can hold only objects.
  • Arrays uses length() to calculate the number of elements, whereas, ArrayLists uses size() to calculate the number of elements.
  • Elements are stored in an Array using the assignment operator, whereas, elements are stored in an ArrayList using add() method.

Read More

Author : Satyam Kumar -



You will also like: