8 Difference between Array and ArrayList in Java with Example

Difference between array and arraylist in java is considered as a starting interview question . This question checks whether candidate know about static and dynamic nature of array.We have already discussed other popular java interview questions like difference between comparable and comparator and difference between arraylist and vector . Difference between array and arraylist in java include eight points namely Resizable, Performance, Traversal ,Primitives , Length , Type-Safety, Adding elements , Multi-dimensional.

1. Resizable : Array is static in size that is fixed length data structure, One can not change the length after creating the Array object.
ArrayList is dynamic in size . Each ArrayList object has instance variable capacity which indicates the size of the ArrayList. As elements are added to an ArrayList its capacity grows automatically.

2. Performance : Performance of Array and ArrayList depends on the operation you are performing :

resize() opertation : Automatic resize of ArrayList will slow down the performance as it will use temporary array to copy elements from the old array to new array.
ArrayList is internally backed by Array during resizing as it calls the native implemented method System.arrayCopy(src,srcPos,dest,destPos,length) .

add() or get() operation : adding an element or retrieving an element from the array or arraylist object has almost same performance , as for ArrayList object these operations run in constant time.

3. Primitives : ArrayList can not contains primitive data types (like int , float , double) it can only contains Object while Array can contain both primitive data types as well as objects.
One get a misconception that we can store primitives(int,float,double) in ArrayList , but it is not true

4. Iterating the values : We can use iterator to iterate through ArrayList . The iterators returned by the ArrayList class's iterator and listiterator method are fail-fast. We can use for loop or for each loop to iterate through array .

5. Type-Safety : In Java , one can ensure Type Safety through Generics. while Array is a homogeneous data structure , thus it will contain objects of specific class or primitives of specific data type. In array if one try to store the different data type other than the specified while creating the array object , ArrayStoreException is thrown.

Similarities Between Array and ArrayList1. add and get method : Performance of Array and ArrayList are similar for the add and get operations .Both operations runs in constant time.2. Duplicate elements : Both array and arraylist can contain duplicate elements.

3. Null Values : Both can store null values and uses index to refer to their elements.

4. Unordered : Both does not guarantee ordered elements.

Recap : Difference between Array and ArrayList in Java

Array

ArrayList

Resizable

No

Yes

Primitives

Yes

No

Iterating values

for, for each

Iterator , for each

Length

length variable

size method

Performance

Fast

Slow in comparision

Multidimensional

Yes

No

Add Elements

Assignment operator

add method

In case you have any doubts regarding the difference between array and arraylist in java then please mention in comments.

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry