Difference Between Array and ArrayList
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.
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());
}
}Array: [91, 88, 95], length 3
ArrayList: [91, 88, 95, 79], size 4Explanation: 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
| Array | ArrayList | |
|---|---|---|
| Size | Fixed when created | Dynamic: grows and shrinks |
| Element types | Primitives (int, double) and objects | Objects only; primitives are autoboxed (Integer) |
| Count of elements | length field | size() method |
| Adding an element | Not possible past the size; create a new array | add(e) |
| Removing an element | Not possible; shift elements yourself | remove(index) or remove(object) |
| Generics | No (int[], String[]) | Yes (ArrayList<String>) |
| Multidimensional | Yes: int[][] | Lists of lists: List<List<Integer>> |
| Methods | Very few; use the Arrays class | Many built in: contains, indexOf, sort |
| Performance | Slightly faster, less memory, especially for primitives | A little overhead for objects and resizing |
| Printing | Arrays.toString(arr) | System.out.println(list) works directly |
Converting Between Them
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]);
}
}[Navika, Srihitha, Advitha, Anamaneni]
4 AnamaneniExplanation: 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
- Easy: Store five temperatures in an array and in an
ArrayList, then add a sixth to the list. - Harder: Write a method that takes an
int[]and returns anArrayList<Integer>containing only its even numbers.
Key Takeaways
Was this tutorial helpful?