Difference Between Array and ArrayList in Java

In this article, we will learn the difference between Array and ArrayList in Java. This is one of the frequently asked interview questions for beginners.

Difference between Array and ArrayList in Java

Fixed Size vs Dynamic Size 

The size of an Array in Java is fixed when it is created. You need to know the size of the array at the time of its creation and it doesn't change. 
int[] numbers = new int[5]; // Array of size 5
// Adding elements to the array
numbers[0] = 1;
numbers[1] = 2;
// We can't add more than 5 elements, doing so will result in ArrayIndexOutOfBoundsException.
On the other hand, ArrayList is dynamic. It can grow or shrink at runtime as needed, which makes it more flexible when you don't know how many elements you're going to have.
ArrayList<Integer> numbersList = new ArrayList<>(); // ArrayList
// Adding elements to the ArrayList
numbersList.add(1);
numbersList.add(2);
// We can keep adding elements, the ArrayList grows automatically.

Type: Primitives vs Objects 

An Array can contain both primitive data types (int, char, etc.) and objects, while an ArrayList only accepts objects, not primitive data types.

int[] intArray = new int[5]; // Array with primitive type
ArrayList<Integer> intArrayList = new ArrayList<>(); // ArrayList with object type

Performance 

An Array is faster and uses less memory as it does not offer dynamic resizing, but when you need the dynamic nature, ArrayList is more efficient because it automatically resizes itself if it gets full.

As per my experience using Array and ArrayList, here are a few important points:
Memory usage: An Array is generally more memory-efficient than an ArrayList. This is because ArrayList, in order to maintain its dynamic size, uses more memory than Array to store its elements.

Processing speed: For basic operations, such as accessing elements at a given index, Arrays can be slightly faster than ArrayLists because arrays are a simple data structure with elements stored in a contiguous block of memory. But this difference is generally negligible and would only become noticeable with a very large number of elements. 

Adding or removing elements: Here is where ArrayLists shine. If you need to frequently add elements to or remove elements from your list, especially at the end of the list, then an ArrayList is likely to outperform an Array. This is because resizing an Array is a costly operation, as it involves creating a new array and copying elements from the old array to the new one. 

Traversing elements: Both Array and ArrayList provide similar performance when it comes to traversing elements. However, if you are using features of the Java Collections Framework (like Iterator, algorithms, etc), ArrayList provides a much richer and more convenient interface.

Flexibility

ArrayList is more flexible because it comes with built-in methods for adding, removing, and querying elements provided by the Collection API, whereas with arrays, you have to manually code these functionalities.

For example, ArrayList provides add() method to insert an element into ArrayList and we can use the assignment operator to store elements into Array.

ArrayList.add() method:
        List<String> animals = new ArrayList<>();
        // Adding new elements to the ArrayList
        animals.add("Lion");
        animals.add("Tiger");
Assignment operator to store elements into Array:
// initialize primitive one dimensional array
int[] anArray = new int[5];

anArray[0] = 10; // initialize first element
anArray[1] = 20; // initialize second element
anArray[2] = 30; // and so forth

Usage: 

In general, if you know the number of elements in the collection upfront, and this number won't change, an array is a good choice. If you need more flexibility or don't know the number of elements in advance, an ArrayList would be a better option.

In simple words, use an Array when you know that the size is fixed, and use ArrayList when you don't know about the size of elements.

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

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

Leave Comment