What is the Difference Between Array and ArrayList in Java ?
The main differences between Array and ArrayList in Java are...
1 - Size : Array in Java is fixed in size. We cannot change the size of array after creating it. ArrayList is dynamic in size. When we add elements to an ArrayList, its capacity increases automatically.
2 - Performance : In Java Array and ArrayList give different performance for different operations.
3 - add() or get() : Adding an element to or retrieving an element from an array or ArrayList object has similar performance. These are constant time operations.
4 - resize() : Automatic resize of ArrayList slows down the performance. ArrayList is internally backed by an Array. In resize() a temporary array is used to copy elements from old array to new array.
5 - Primitives : Array can contain both primitive data types as well as objects. But ArrayList cannot contain primitive data types. It contains only objects.
6 - Iterator : In an ArrayList we use an Iterator object to traverse the elements. We use for loop for iterating elements in an array.
7 - Type Safety : Java helps in ensuring Type Safety of elements in an ArrayList by using Generics. An Array can contain objects of same type of class. If we try to store a different data type object in an Array then it throws ArrayStoreException.
8 - Length : Size of ArrayList can be obtained by using size() method. Every array object has length variable that is same as the length/size of the array.
9 - Adding elements : In an ArrayList we can use add() method to add objects. In an Array assignment operator is used for adding elements.
10 - Multi-dimension : An Array can be multi-dimensional. An ArrayList is always of single dimension.