Difference between Array vs ArrayList in Java

In this post, we will discuss the important topic that is the difference between Array and ArrayList in Java.

Difference of Array and ArrayList

  1. Array is a fixed-length data structure that is you can not change the length of Array once created in Java whereas ArrayList is a variable-length Collection class that is ArrayList re-size itself when gets full depending upon the capacity and load factor.
  2. We can not use Generics along with Array, as Array instance knows about what kind of type it can hold and throws ArrayStoreException, if you try to store type which is not convertible into the type of ArrayArrayList allows you to use Generics to ensure type-safety.
  3. Array can contain both primitive data types as well as objects of a class depending on the definition of the array whereas ArrayList only supports object entries, not the primitive data types.
  4. Java provides add() method to insert an element into ArrayList and we can use the assignment operator to store elements into Array.
  5. When creating an Array object, it's mandatory to provide the size of Array but when creating ArrayListobject, no need to pass the size, if the size is not passed then ArrayList internally initialized with a default size of 10.
  6. Array can be multidimensional but ArrayList is always single-dimensional.
  7. Use Array, when you know that the size is fixed and use ArrayList, when you don't know about the size of elements.
Let's understand more about Array and ArrayList with performances, operations, when to use ?, similarities and examples.

Performance of Array and ArrayList

Performance of Array and ArrayList depends on the operation you are performing :

resize() operation :

Automatic resize of ArrayList will slow down the performance as it will use a temporary array to copy elements from the old array to the new array. ArrayList is internally backed by Array during resizing as it calls the native implemented method System.arrayCopy(src,srcPos,dest,destPos,length) 

add() or get() operation :

adding an element or retrieving an element from the array or ArrayList object has almost the same performance , as for ArrayList object these operations run in constant time.

Array and ArrayList Example

public class ArrayArrayListExample {
    public static void main(String[] args) {
        // Creating an ArrayList of String using
     List<String> animals = new ArrayList<>();
        // Adding new elements to the ArrayList
        String[] arrayOfAnimals = new String[4];
        arrayOfAnimals[0] = "Lion";
        arrayOfAnimals[1] = "Tiger";
        arrayOfAnimals[2] = "Cat";
        arrayOfAnimals[3] = "Dog";
        for (String string : arrayOfAnimals) {
[Lion, Tiger, Cat, Dog]

When can I use Arrays and ArrayList?

Arrays can be used when you have a fixed number of elements to work on and you’re sure this number is never going to change. Arrays also offer a neat way of storing data in multiple rows and columns (multi-dimensional arrays).
But even with that, you can still simultaneously enjoy the strength of an Array and the strength of an ArrayList because Java allows you to convert from an Array to an ArrayList and vice-versa.To convert from an ArrayList to an Array, use the toArray() method. To convert from an Array to an ArrayList, use the Array.asList() method.

Similarities Between Array and ArrayList

  1. add and get method : Performance of Array and ArrayList are similar for the add and get operations .Both operations run in constant time.
  2. Duplicate elements : Both array and ArrayList can contain duplicate elements.
  3. Null Values : Both can store null values and uses an index to refer to their elements
You should read How the Size of the ArrayList Increases Dynamically?


  1. how can you say that arrays is unordered

    Unordered : Both does not guarantee ordered elements.

    1. You are correct, Array and ArrayList preserve the insertion order. This point may be removed.


Post a Comment