Key Differences Between Array and ArrayList in Java

In this topic, we will learn about the differences between Array and ArrayList in Java

What is an Array in Java?

An array is an indexed collection of a fixed number of homogeneous data types of elements. It is a linear data structure that provides the functionality to add elements in a continuous manner in memory address space.

Syntax of creating Array

Type array_name[ ] = new Type[array_size];

What is an ArrayList in Java?

ArrayList is a collection of heterogeneous objects. It stores objects on the index-based and the underlying data structure is a resizable or growable array.

Syntax of creating ArrayList

ArrayList<Type> al = new ArrayList<Type>();

Array vs ArrayList in Java

                           Array                                  ArrayList
It is a fixed in size.It is dynamic in size. It can grow and reduce size dynamically based on the requirements.
We need to specify the size of the array when we are declaring the array. Then we cannot change the size of the array once specified.No need to specify the size of the ArrayList in a declaration and the size of the ArrayList is changed dynamically.
It gives better performance as compared to ArrayList because the size of the array is fixed and each element of the array can be accessed using the index.An ArrayList needs to adjust the size once the initial size of the ArrayList is reached. This can decrease the performance of the ArrayList. The dynamically growing and reducing the size of the ArrayList affects the overall performance of the ArrayList when we perform several operations(add, delete etc.).
It is not type-safe.It is a type-safe.
It can be multidimensional(2D array, 3D array etc.).It is single-dimensional.
We can use an assignment operator to initialize an array(arr[0]=100).We can use several ways to initialize an ArrayList.
We can know the size of the array by using(arr.length).We can know the size of the ArrayList by using the size()  method of the ArrayList class (al.size()).
We can iterate the array by using for and enhanced for loops.We can iterate the ArrayList class by using the for loop, enhanced for loop, Iterator and ListIterator.
There is no underlying data structure for arrays and hence there is no readymade method support available.An underlying data structure is a resizable array or growable array. 
It can hold both primitives and object types.It can hold only objects.

Similarities between Array and ArrayList

There are the following similarities between Array and ArrayList:
    • Both of the performances are similar for the add operation and get operation.
    • Both can hold duplicate elements.
    • Both can allow null values.
    • Both are default unsorted orders. We can sort arrays by applying logic. Various methods to sort ArrayList in Java.

Java Array Example

public class SpringJava{
public static void main(String args[]){
//An int array that can store 5 elements
//name of an array is "num"
int num[]=new int[5];
//storing values in the array
num[0]=100;//1st element
num[1]=200;//2nd element
num[2]=300;//3rd element
num[3]=500;//4th element
num[4]=30;//5th element
//printing the elements of an array "num"
for(int i=0;i<num.length;i++){
System.out.println("num["+i+"]: "+num[i]);
    }
  }
}

Output

num[0]: 100
num[1]: 200
num[2]: 300
num[3]: 500
num[4]: 30

Java ArrayList Example

import java.util.ArrayList;
public class SpringJava{
public static void main(String args[]){
//creating an ArrayList of String type
//name of an arraylist is "al"
ArrayList<Object> al = new ArrayList<>();
//adding elements to the arraylist using add() method
al.add("Spring");
al.add("Java");
al.add("Website");
al.add("Tutorials");
al.add(10);
//printing the elements of the arraylist
for(Object obj:al){
System.out.println(obj);
}
//printing the ArrayList
System.out.println(al);
  }
}

Output

Spring
Java
Website
Tutorials
10
[Spring, Java, Website, Tutorials, 10]

FAQ

Q1: Which is better to use array or ArrayList?
Ans. An ArrayList is easy to implement and the use of an array provides more control over data.
Q2: What is the reason to use ArrayList instead of Array?
Ans. An Array is more flexible to use as compared to an array because of its dynamic growth and reduction of the size and underlying data structure support methods.

Conclusion

In this topic, we learnt what are the difference between array and ArrayList with examples.

Leave a Comment