Difference Between Array and ArrayList

Naveen AnamaneniUpdated · 4 min read

An array and an ArrayList both store an ordered group of elements that can be accessed by index. The main difference is that an array has a fixed size, decided when it is created, while an ArrayList is dynamic: it grows and shrinks as elements are added and removed. In this lesson, you will learn all the differences between them, with an example of each.

Example: Array and ArrayList Side by Side

Let's see an example that stores marks both ways.

ArrayVsList.java▶ Try it
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class ArrayVsList {
    public static void main(String[] args) {
        int[] marksArray = new int[3];
        marksArray[0] = 91;
        marksArray[1] = 88;
        marksArray[2] = 95;
        System.out.println("Array: " + Arrays.toString(marksArray)
            + ", length " + marksArray.length);
 
        List<Integer> marksList = new ArrayList<>();
        marksList.add(91);
        marksList.add(88);
        marksList.add(95);
        marksList.add(79);
        System.out.println("ArrayList: " + marksList
            + ", size " + marksList.size());
    }
}
Output
Array: [91, 88, 95], length 3
ArrayList: [91, 88, 95, 79], size 4

Explanation: The array was created for exactly 3 marks; a fourth would need a new, bigger array. The ArrayList simply accepts a fourth mark with add. Note the different ways to get the count: length (a field) for arrays and size() (a method) for lists.

Differences Between Array and ArrayList

ArrayArrayList
SizeFixed when createdDynamic: grows and shrinks
Element typesPrimitives (int, double) and objectsObjects only; primitives are autoboxed (Integer)
Count of elementslength fieldsize() method
Adding an elementNot possible past the size; create a new arrayadd(e)
Removing an elementNot possible; shift elements yourselfremove(index) or remove(object)
GenericsNo (int[], String[])Yes (ArrayList<String>)
MultidimensionalYes: int[][]Lists of lists: List<List<Integer>>
MethodsVery few; use the Arrays classMany built in: contains, indexOf, sort
PerformanceSlightly faster, less memory, especially for primitivesA little overhead for objects and resizing
PrintingArrays.toString(arr)System.out.println(list) works directly

Converting Between Them

Convert.java▶ Try it
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class Convert {
    public static void main(String[] args) {
        String[] namesArray = {"Navika", "Srihitha", "Advitha"};
        List<String> namesList = new ArrayList<>(Arrays.asList(namesArray));
        namesList.add("Anamaneni");
        System.out.println(namesList);
 
        String[] back = namesList.toArray(new String[0]);
        System.out.println(back.length + " " + back[3]);
    }
}
Output
[Navika, Srihitha, Advitha, Anamaneni]
4 Anamaneni

Explanation: Arrays.asList wraps the array as a fixed-size list, and new ArrayList<>(...) copies it into a real, growable list. toArray(new String[0]) converts the list back into a String[] of the right size.

Common Mistakes

Calling arr.size() or list.length. Arrays use length; lists use size().

Writing ArrayList<int>. Use ArrayList<Integer>.

Adding to the list from Arrays.asList directly. It's fixed-size; copy it into a new ArrayList<>(...) first.

Interview Corner

What is the difference between an array and an ArrayList?

An array has a fixed length set at creation, can hold primitives or objects, exposes its length through the length field, and offers few built-in operations. An ArrayList is a resizable List implementation that grows automatically, holds only objects (primitives are autoboxed), reports its size through size(), supports generics and offers many methods such as add, remove and contains. Arrays are slightly faster and lighter, especially for primitives.

Practice

  1. Easy: Store five temperatures in an array and in an ArrayList, then add a sixth to the list.
  2. Harder: Write a method that takes an int[] and returns an ArrayList<Integer> containing only its even numbers.

Quick check: Array vs ArrayList

4 questions
1Which has a fixed size?
2How do you get the number of elements in an ArrayList?
3Can an ArrayList hold int values directly?
4Which converts an ArrayList to an array?

Key Takeaways

Was this tutorial helpful?

Lesson 260 of 482