Criteria Array ArrayList
Size Fixed. The size of an array is set upon initialization and cannot be changed. Dynamic. The size can change as elements are added or removed.
Type Can store both primitives and objects. Stores only objects. Primitives need to be wrapped (e.g., int as Integer).
Performance Generally faster, as there is no overhead of resizing. May involve overhead due to dynamic resizing.
Methods Limited operations, mainly index-based value setting and retrieval. Offers numerous methods for operations like add(), remove(), get(), set(), and many more.
Flexibility Less flexible due to fixed size. More flexible due to dynamic resizing and variety of built-in methods.
Thread Safety Not inherently thread-safe. Not thread-safe. Consider using Vector or synchronizing externally if thread safety is required.
Memory Overhead Lower overhead, as memory is allocated only for the array’s elements and a fixed length. Higher overhead due to dynamic resizing and the underlying data structure.
Use-case When size is known in advance and remains constant, or for performance-critical scenarios. When flexibility is needed in terms of size, or when you need to use built-in methods for manipulating data.

Example: Difference Between Array and ArrayList in Java

import java.util.ArrayList;

public class ArrayVsArrayListDemo {

    public static void main(String[] args) {

        // Using Array
        int[] intArray = new int[3];
        intArray[0] = 1;
        intArray[1] = 2;
        intArray[2] = 3;
        System.out.println("Array Element at Index 1: " + intArray[1]);

        // Using ArrayList
        ArrayList<Integer> intList = new ArrayList<>();
        intList.add(1);
        intList.add(2);
        intList.add(3);
        System.out.println("ArrayList Element at Index 1: " + intList.get(1));
    }
}

Output:

Array Element at Index 1: 2
ArrayList Element at Index 1: 2

Explanation:

1. Array: An array is a basic data structure to store elements of the same type. The size of an array is fixed once it's created, and you cannot change it. Arrays can be of primitive data types (like int, char) or objects.

2. ArrayList: It is a part of the Java Collections Framework and is present in java.util package. Unlike arrays, ArrayLists are dynamic and can grow or shrink as needed. They can only store object types, not primitives (but can use wrapper classes like Integer for int).

3. In the provided example:

– For Array: A simple integer array of size 3 is created, and values are assigned using indices.

– For ArrayList: An ArrayList of type Integer is created, and values are added using the add() method. Retrieval is done using the get() method.

4. When to use:

– Use Array when the size is known in advance and won't change, or when you want to use primitive data types.

– Use ArrayList when you need a resizable array or need methods and capabilities provided by the Collections framework.

In summary, while both Array and ArrayList can be used to store elements, Array is a fixed-size and simpler data structure, while ArrayList offers dynamic resizing and a richer set of operations.