Difference between an ArrayList and an Array in java.

Modified on Tue, 28 Mar, 2023 at 3:51 PM

In Java, an array is a fixed-size data structure that stores elements of the same data type in contiguous memory locations, while an ArrayList is a dynamic-size data structure that stores elements of the same data type in a resizable array.

Here are some key differences between an array and an ArrayList in Java:

Size: The size of an array is fixed and determined at compile time, while the size of an ArrayList is dynamic and can change at runtime.

Memory allocation: Arrays are allocated on the stack or the heap, depending on where they are declared, while ArrayLists are always allocated on the heap.

Type safety: Arrays are not type safe, meaning that you can store elements of different types in the same array. ArrayLists are type safe, meaning that you can only store elements of the same data type in an ArrayList.

Performance: Accessing elements of an array is faster than accessing elements of an ArrayList, as arrays are stored in contiguous memory locations. However, adding or removing elements from an ArrayList is faster than adding or removing elements from an array, especially if the array needs to be resized.


Syntax: To declare an array, you use square brackets after the data type, like this:

 int[] myArray = new int[10];

// datatype[] variable_name = new datatype[size];


Syntax: To declare an ArrayList, you use angle brackets after the data type, like this: 

ArrayList<Integer> myList = new ArrayList<Integer>();.

// ArrayList<datatype> list_name = new ArrayList<datatype>();


Overall, the choice between using an array or an ArrayList depends on the specific requirements of your program. If you need a fixed-size data structure with fast access to elements, use an array. If you need a dynamic-size data structure that can be resized easily, use an ArrayList.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article