Difference between Array vs ArrayList in Java - RameshMF/java-interview GitHub Wiki
Array | ArrayList |
---|---|
Array is a fixed length data structure that is you can not change length of Array once created in Java | ArrayList is a variable length Collection class that is ArrayList re-size itself when gets full depending upon capacity and load factor |
We can not use Generics along with Array, as Array instance knows about what kind of type it can hold and throws ArrayStoreException, if you try to store type which is not convertible into type of Array | ArrayList allows you to use Generics to ensure type-safety |
Array can contain both primitive data types as well as objects of a class depending on the definition of the array | ArrayList only supports object entries, not the primitive data types |
Java provides add() method to insert element into ArrayList | We use assignment operator to store element into Array |
Its mandatory to provide size of Array | We can create ArrayList with default size of 10 |
Each array object has the length variable which returns the length of the array | Length of the ArrayList is provided by the size() method |
Array can be multi dimensional | ArrayList is always single dimensional |
Use Array, when you know that the size is fixed | Use ArrayList, when you don't know about the size of elements |
-
Array is a fixed length data structure that is you can not change length of Array once created in Java where as ArrayList is a variable length Collection class that is ArrayList re-size itself when gets full depending upon capacity and load factor.
-
We can not use Generics along with Array, as Array instance knows about what kind of type it can hold and throws ArrayStoreException, if you try to store type which is not convertible into type of Array. ArrayList allows you to use Generics to ensure type-safety.
-
Array can contain both primitive data types as well as objects of a class depending on the definition of the array where as ArrayList only supports object entries, not the primitive data types.
-
Java provides add() method to insert element into ArrayList and we can use assignment operator to store element into Array.
-
When creating Array object, its mandatory to provide size of Array but when creating ArrayList object, no need to pass the size, if size is not passed then ArrayList internally initialized with default size of 10.
-
Array can be multi dimensional but ArrayList is always single dimensional.
-
Use Array, when you know that the size is fixe and use ArrayList, when you don't know about the size of elements.
Let's understand more about Array and ArrayList with examples.
Performance of Array and ArrayList depends on the operation you are performing :
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) .
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.
public class ArrayArrayListExample {
public static void main(String[] args) {
// Creating an ArrayList of String using
List<String> animals = new ArrayList<>();
// Adding new elements to the ArrayList
animals.add("Lion");
animals.add("Tiger");
animals.add("Cat");
animals.add("Dog");
System.out.println(animals);
String[] arrayOfAnimals = new String[4];
System.out.println(arrayOfAnimals.length);
arrayOfAnimals[0] = "Lion";
arrayOfAnimals[1] = "Tiger";
arrayOfAnimals[2] = "Cat";
arrayOfAnimals[3] = "Dog";
for (String string : arrayOfAnimals) {
System.out.println(string);
}
}
}
Output:
[Lion, Tiger, Cat, Dog]
4
Lion
Tiger
Cat
Dog
Arrays can be used when you have a fixed number of elements to work on and you’re sure this number is never going to change. Arrays also offer a neat way of storing data in multiple rows and columns (multi-dimensional arrays).
But even with that, you can still simultaneously enjoy the strength of an Array and the strength of an ArrayList because Java allows you to convert from an Array to an ArrayList and vice-versa.To convert from an ArrayList to an Array, use the toArray() method. To convert from an Array to an ArrayList, use the Array.asList() method.
-
add and get method : Performance of Array and ArrayList are similar for the add and get operations .Both operations runs in constant time.
-
Duplicate elements : Both array and arraylist can contain duplicate elements.
-
Null Values : Both can store null values and uses index to refer to their elements.
-
Unordered : Both does not guarantee ordered elements.