Difference between Array and ArrayList in Java
Hey everyone, welcome back to my blog! In today’s post, we will talk about two essential data structures in Java: Array and ArrayList. They may seem similar at first, but they have some major differences that can affect how and when you use them. So, let’s break it down!
This is one of the frequently asked interview questions for beginners in the interview. Make sure to explain each important difference discussed in this blog post.
Learn everything about Java: https://www.rameshfadatare.com/learn-java-programming/
1. Arrays are static in nature vs. ArrayList is dynamic in nature
First, let’s talk about the core difference in how these two are structured. Arrays are static, meaning once you create an array, you have to specify its size up front, and this size is fixed. You cannot change it later.
Example for Array:
import java.util.Arrays;
public class ArrayExample {
public static void main(String[] args) {
int[] numbers = new int[5]; // Array size is fixed at 5
numbers[0] = 10;
numbers[1] = 20;
System.out.println(Arrays.toString(numbers)); // Output: [10, 20, 0, 0, 0]
}
}
Output:
[10, 20, 0, 0, 0]
On the other hand, ArrayList is dynamic, meaning you don’t need to specify a fixed size. It grows and shrinks as you add or remove elements. This makes ArrayList much more flexible for scenarios where the number of elements is not known in advance.”
Example for ArrayList:
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<Integer> numbersList = new ArrayList<>();
numbersList.add(10); // Dynamic resizing, size adjusts automatically
numbersList.add(20); // Adding more elements as needed
System.out.println(numbersList); // Output: [10, 20]
}
}
Output:
[10, 20]
2. Array holds both primitives and objects vs. ArrayList holds only objects
Another key difference is what type of data these structures can hold. Arrays can store primitive data types like int, char, and boolean, as well as objects like String and Integer. This makes arrays versatile.
Example for Array with primitives and objects:
import java.util.Arrays;
public class ArrayPrimitiveObject {
public static void main(String[] args) {
int[] primitiveArray = {1, 2, 3}; // Storing primitive data
String[] objectArray = {"Java", "Python"}; // Storing objects
System.out.println(Arrays.toString(primitiveArray)); // Output: [1, 2, 3]
System.out.println(Arrays.toString(objectArray)); // Output: [Java, Python]
}
}
Output:
[1, 2, 3]
[Java, Python]
However, ArrayList can only store objects. This means that if you try to store primitive data types, they’ll automatically get converted to their respective wrapper classes — like int becomes Integer. This process is called auto-boxing.
Example for ArrayList:
import java.util.ArrayList;
public class ArrayListPrimitiveObject {
public static void main(String[] args) {
ArrayList<Integer> integerList = new ArrayList<>();
integerList.add(1); // Auto-boxing happens: int is converted to Integer
integerList.add(2);
System.out.println(integerList); // Output: [1, 2]
}
}
Output:
[1, 2]
3. Arrays don’t support generics vs. ArrayList supports generics
Arrays don’t support generics. This means you can’t specify the data type when you create an array, and it can hold mixed data types.”
Example for Array:
import java.util.Arrays;
public class ArrayGenericsExample {
public static void main(String[] args) {
Object[] mixedArray = {1, "Hello", 3.14}; // Can hold different data types
System.out.println(Arrays.toString(mixedArray)); // Output: [1, Hello, 3.14]
}
}
Output:
[1, Hello, 3.14]
In contrast, ArrayList supports generics, meaning you can specify the type of objects it should store. This ensures type safety.
Example for ArrayList:
import java.util.ArrayList;
public class ArrayListGenericsExample {
public static void main(String[] args) {
ArrayList<String> stringList = new ArrayList<>();
stringList.add("Java"); // Only String objects allowed
// stringList.add(123); // Uncommenting this line would cause a compile-time error
System.out.println(stringList); // Output: [Java]
}
}
Output:
[Java]
4. Single-dimensional or multidimensional vs. Only single-dimensional
Arrays are versatile and can be single-dimensional or multi-dimensional, making them useful for structures like grids or matrices.”
Example for Multidimensional Array:
import java.util.Arrays;
public class MultiDimensionalArrayExample {
public static void main(String[] args) {
int[][] matrix = {{1, 2}, {3, 4}}; // Multidimensional array (2x2 grid)
System.out.println(Arrays.deepToString(matrix)); // Output: [[1, 2], [3, 4]]
}
}
Output:
[[1, 2], [3, 4]]
ArrayLists are strictly single-dimensional. You can create a multi-dimensional structure using a list of lists, but it’s more complex.
Example for Nested ArrayList:
import java.util.ArrayList;
public class NestedArrayListExample {
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> nestedList = new ArrayList<>();
ArrayList<Integer> innerList = new ArrayList<>();
innerList.add(1);
innerList.add(2);
nestedList.add(innerList); // Nested ArrayList to mimic multidimensional behavior
System.out.println(nestedList); // Output: [[1, 2]]
}
}
Output:
[[1, 2]]
5. Arrays generally offer better performance vs. ArrayList can be slower, especially when increasing the size
Arrays have a fixed size and simple structure, making element access very fast. It takes constant time to access an element by index.
Example for Array Performance:
public class ArrayPerformanceExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[2]); // Output: 3, constant time access
}
}
Output:
3
ArrayLists can be slower when growing beyond their capacity because they have to resize internally.
Example for ArrayList Performance:
import java.util.ArrayList;
public class ArrayListPerformanceExample {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
list.add(i); // Performance may slow down as it resizes
}
System.out.println("Final size: " + list.size()); // Output: Final size: 1000
}
}
Output:
Final size: 1000
Conclusion
That wraps up our comparison between Array and ArrayList! To recap: Arrays are static, support both primitives and objects and offer better performance but less flexibility. On the other hand, ArrayList is dynamic, holds only objects, and provides more flexibility, though at a slight performance cost. I hope this helped clarify the differences between these two structures.