10 Difference between Array vs ArrayList in Java - ayushmathur94/DirectQuesAns_Prep GitHub Wiki

1. Implementation

The array is a native programming component or a data structure but ArrayList is a class from the Java Collections framework, an API. In fact, ArrayList is internally implemented using an array in Java. Since ArrayList is a class it holds all properties of a class like you can create objects and call methods but even though the array is an object in Java it doesn't provide any method. It just exposes a length attribute to give you the length of the array, which is constant.

2. Performance

Since ArrayList is based upon an array you would assume that it provides the same performance as an array. This is true to some extent but because of the extra functionality ArrayList provides there is some difference in the performance of ArrayList and array, mainly in terms of memory usage and CPU time.

For index-based access, both ArrayList and array provide O(1) performance but add can be O(logN) in ArrayList if adding a new element triggers resize, as it involves creating a new array in the background and copying elements from the old array to new array.

The memory requirement for ArrayList is also more than an array for storing the same number of objects like an int[] will take less memory to store 20 int variables than an ArrayList because of object metadata overhead on both ArrayList and wrapper class.

3. Type Safety

Now, the difference between ArrayList and array is around type safety, which means whether you can store integers on a String array or not. ArrayList is type-safe because it supports generics which allows the compiler to check if all objects stored in ArrayList are of the correct type.

On the other hand, the array doesn't support Generics in Java. This means compile-time checking is not possible but array provides runtime type checking by throwing ArrayStoreException if you try to store an incorrect object into an array, like storing a String into an int array.

4. Flexibility

ArrayList is more flexible than a plain native array because it's dynamic. It can grow itself when needed, which is not possible with the native array.

ArrayList also allows you to remove elements that are not possible with native arrays. By remove, we mean not just assigning null to the corresponding index but also copying the rest of the elements one index down, which ArrayList automatically does for you.

5. Primitives

If you first start using ArrayList then you will realize that you cannot store primitives on ArrayList. This is a key difference between array and ArrayList because array allows storing both primitives and objects.

For example, int[] numbers are valid but ArrayList of int is not valid. how do you deal with this problem? Suppose you want to store int primitives into ArrayList then how do you that? Well, you can use the wrapper class.

This is one of the reasons why the wrapper class was introduced in Java. So if you want to store int 2 into ArrayList just put it, autoboxing will do the rest. Btw, this difference is not so obvious from Java 5 onwards because of auto-boxing as you will see that ArrayList.add(21) is perfectly valid and works.

6. Generics

One more significant difference between an ArrayList and an array is that the former supports Generics but later doesn't. Since an array is of covariant type, you can use Generics with them. This means it's not possible for a compiler to check the type-safety of an array at compile time but they can verify the type-safety of Array.

So how do you deal with this problem while writing a type-safe class in Java? Well, you can use the technique shown in Effective Java, where you can declare an array, like E[] and later use typecasting.

7. Iteration

ArrayList provides more ways for iteration i.e. accessing all elements one by one than an array. You can only use loops like for loop, while loop, enhanced for loop and do-while to iterate over an array but you can also use the Iterator and ListIterator class to iterate over ArrayList. See here to learn different ways to iterate over ArrayList in Java.

8.Size() vs length

Array only provides a length attribute that tells you the number of slots in the array i.e. how many elements it can store, it doesn't provide you any method to find out how many are filled and how many slots are empty i.e. the current number of elements.

While ArrayList does provide a size() method which tells the number of objects stored in ArrayList at a given point of time. The size() is always different than the length, which is also the capacity of ArrayList.

9. Dimension

Another significant difference between an array and an ArrayList is that array can be multi-dimensional like you can have a two-dimensional array or a three-dimensional array, which makes it a really special data structure to represent matrices and 2D terrains.

On the other hand, ArrayList doesn't allow you to specify dimensions.