Difference between Array and ArrayList in Java

Here you will learn about difference between array and arraylist in java.

Both array and arraylist are most important and frequently used data structure in java. Below I have discussed about various differences between them.

 

Difference between Array and ArrayList in Java
Image Source

Difference between Array and ArrayList in Java

Property Array ArrayList
Size Array is fixed in size. Once declared, its size can’t be changed. ArrayList size can be changed after declaration. For example when we add or remove element in arraylist, its size increases or decreases accordingly.
Primitives Array can contain primitive data types as well as objects. ArrayList can contain objects only. When we try to add a primitive type element in arraylist then the element is first converted into its corresponding wrapper class and then added. This process of conversion is called auto-boxing.
Performance Array provides better performance and uses less memory. ArrayList performance is less and uses more memory as compared to Array. ArrayList internally uses dynamic array for storing elements. Each time an element is added or removed, a new array is created.
Element Type Array can contain elements of same type. ArrayList can contain elements of different types.
Iteration We can iterate through array using loops only. ArralyList provides various ways for iteration like loops, Iterator and ListIterator class.
Dimension Array can be single as well as multi-dimensional. ArrayList is only single dimensional.
Storage Array elements are stored at contiguous memory locations. ArrayList elements are not stored at contiguous memory locations.

 

Array and ArrayList Example in Java

Here I have shared one example that will help you to understand the working of array and arraylist.

 

Output

10 15 20

10 15 20

 

Comment below if you found anything incorrect in above tutorial for difference between array and arraylist in java.

Leave a Comment

Your email address will not be published. Required fields are marked *