Difference between Array vs ArrayList in Java

In this post, we will discuss the important topic that is 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 length of Array once created in Java where as ArrayList is a variable length Collection class that is ArrayList re-size itself when gets full depending upon 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 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 where as ArrayList only supports object entries, not the primitive data types.
  4. Java provides add() method to insert element into ArrayList and we can use assignment operator to store element into Array.
  5. When creating Array object, its mandatory to provide size of Array but when  creating ArrayListobject, no need to pass the size, if size is not passed then ArrayList internally initialized with default size of 10.
  6. Array can be multi dimensional 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() opertation :

Automatic resize of ArrayList will slow down the performance as it will use temporary array to copy elements from the old array to 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 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
        animals.add("Lion");
        animals.add("Tiger");
        animals.add("Cat");
        animals.add("Dog");
        System.out.println(animals);
        
        String[] arrayOfAnimals = new String[4];
        System.out.println(arrayOfAnimals.length);
        arrayOfAnimals[0] = "Lion";
        arrayOfAnimals[1] = "Tiger";
        arrayOfAnimals[2] = "Cat";
        arrayOfAnimals[3] = "Dog";
        for (String string : arrayOfAnimals) {
         System.out.println(string);
        }
        
    }
}
Output:
[Lion, Tiger, Cat, Dog]
4
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 runs in constant time.
  2. Duplicate elements : Both array and arraylist can contain duplicate elements.
  3. Null Values : Both can store null values and uses index to refer to their elements.

  1. You should read How the Size of the ArrayList Increases Dynamically?

Comments

  1. how can you say that arrays is unordered

    Unordered : Both does not guarantee ordered elements.

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

      Delete

Post a Comment