Main differences between Array and ArrayList in java

How is an array different from ArrayList ? What are the major differences between Array and ArrayList ? Why difference between Array and ArrayList is the most commonly asked Java Interview Question . Below given are the points for Array vs ArrayList which describes it.

S. No     Array     ArrayList
1 Array is fixed length , thus static in nature ArrayList  is dynamic in nature
2 Array does not extend any class or implement interface ArrayList implements the List Interface and internally backed by Array
3 Array does not support Generics and throws ArrayStoreException ArrayList supports Generics and ensures type-safety.
4

Array provides length() method to calculate

the Array Length

Example:

int studentObject[] = new Integer[3];int
int studentObjectLength= studentObject.length ;

ArrayList provides size() method to calculate the ArrayList size

Example:
ArrayList studentObject = new ArrayList();
studentObject.add(101);
studentObject.add(102);
studentObject.size();

5 Arrays allows to store primitive types and objects ArrayList allows to store objects only
6

Array uses assignment operator to store element into array

Example:

int[] studentId = { 101, 102,103,104,105 };

Object[] studentId = new Object[10];

ArrayList uses add() method to store element in ArrayList

Example:

ArrayList<Integer> studentIdList = new ArrayList<Integer>();
studentIdList.add(100);

7

Arrays are faster when frequent access is required.

Array operations execution time

Arrays Lookup –  O(1)

Arrays Append ( last element) – O(1)

Array Insertion – O(n)

Array Deletion – O(n)

ArrayList is faster when frequent insert and deletion is required.  Overall ArrayList takes  more time as it uses List interface as part of Collection Framework and each node in the List has its own memory when compared to Arrays.
8

Array is traversed using for loop

ArrayList is traversed using Iterator

 

Comments are closed.