Array vs ArrayList in Java

In this post, we will learn the difference between Array and ArrayList in Java. This is a frequently asked
question in Java interviews for beginners. Let's dive into it.

Difference between Array and ArrayList in Java

Features Array ArrayList
Nature Array is static in nature. Array is a fixed-length data structure. You can’t change their size once they are created. ArrayList is dynamic in nature. Its size is automatically increased if you add elements beyond its capacity.
Element Type Array can hold both primitives as well as objects. ArrayList can hold only objects.
Iteration Array can be iterated only through a for loop or for-each loop. ArrayList provides iterators to iterate through their elements.
Size Check The size of an array is checked using the length attribute. The size of an ArrayList can be checked using the size() method.
Performance Array gives constant time performance for both add and get operations. ArrayList also gives constant time performance for both add and get operations provided adding an element doesn’t trigger resize.
Generics Support Array doesn’t support generics. ArrayList supports generics.
Type Safety Array is a not type-safe. ArrayList is type-safe.
Dimensions Array can be multi-dimensional. ArrayList can’t be multi-dimensional.
Element Addition Elements are added using the assignment operator. Elements are added using the add() method.


Using Array:

public class ArrayExample {
    public static void main(String[] args) {
        // Declare and initialize an array of integers
        int[] numbersArray = {1, 2, 3, 4, 5};

        // Modify an element in the array
        numbersArray[2] = 99;

        // Display elements of the array
        for (int num : numbersArray) {
            System.out.print(num + " ");


1 2 99 4 5 

Using ArrayList:

import java.util.ArrayList;

public class ArrayListExample {
    public static void main(String[] args) {
        // Declare and initialize an ArrayList of integers
        ArrayList<Integer> numbersList = new ArrayList<>();

        // Add elements to the ArrayList

        // Modify an element in the ArrayList
        numbersList.set(2, 99);

        // Remove an element
        numbersList.remove(4);  // removes the element at index 4

        // Display elements of the ArrayList
        for (int num : numbersList) {
            System.out.print(num + " ");


1 2 99 4 

Download Cheat Sheet:

Related Collections Interview QA

  1. map() vs flatMap() in Java
  2. Collections vs Streams
  3. ArrayList vs Vector
  4. Iterator vs ListIterator
  5. HashMap vs HashTable
  6. HashSet vs HashMap
  7. Array vs ArrayList
  8. Fail-Fast Iterators vs Fail-Safe Iterators
  9. HashMap vs ConcurrentHashMap
  10. LinkedList vs ArrayDeque
  11. LinkedList vs Array
  12. LinkedList vs Doubly LinkedList
  13. Enum vs EnumSet in Java
  14. HashMap vs. TreeMap in Java
  15. Synchronized Collections vs. Concurrent Collections