In this guide, we will discuss the difference between Array and ArrayList.
What is an Array?
An array is a collection of elements of similar type, stored in contiguous memory locations. It is one of the simplest data structure where you can access each element of the array by only using its index number.
For example: If you want to salary of employees in an array, you can create an array like this:
int salary[] = new int[10];
Now this array will hold the salary values for 10 employees as the size of the array is 10. Here, salary[0]
denotes the salary of first employee and it is the first element of the array. Similarly salary[9]
denotes the salary of 10th employee and it is the last element of the array.
Note: Salary is a number so we created an int array, this is why we said in the beginning that array contains elements of similar type, you can’t store strings in an int array.
What is an ArrayList?
Arraylist is based on an Array data structure. It is widely used because of the functionality and flexibility it offers. Developers prefer Arraylist as it provides more features and easily scalable compared to arrays. ArrayList is a resizable-array.
The limitation of an array is that it has a fixed length so if it is full you cannot add any more elements to it, likewise if there are number of elements gets removed from it the memory consumption would be the same as it doesn’t shrink. On the other ArrayList can dynamically grow and shrink after addition and removal of elements.
Array vs ArrayList
Array | ArrayList |
---|---|
Array has fixed size. | ArrayList is dynamic in size, it can grow and shrink dynamically based on the requirements. |
You need to specify the size of the Array while declaring it, which cannot be changed later. | No need to specify the size during ArrayList declaration and the size is changed automatically. |
Array gives better performance than ArrayList as the size of the array is fixed and each element can be accessed using index. | ArrayList needs to adjust the size once the initial size of the ArrayList is reached and this can hinder the performance of ArrayList. This dynamic growing and shrinking of size can affect the overall performance of ArrayList while performing several operations such as add, delete etc. |
Array is not type safe | ArrayList is type safe, |
Array can be multidimensional such as 2D array, 3d array etc. | ArrayList is single dimensional. |
You can only use assignment operator to initialize an array such as arr[0] =100 . |
You can use multiple ways to initialize an ArrayList. |
Size of the array can be found using length property. For example: arr.length |
Size of the ArrayList can be found using size() method of ArrayList class. For example: al.size() . |
We can use loops to iterate an array. | We can use iterator to loop an ArrayList.. |
Similarities between Array and ArrayList
1. Array and ArrayList both the data structures are used to store similar type of elements.
2. Both can contain duplicate values.
3. Both allow null values.
4. Both Array and ArrayList are by default unsorted. You can however sort array and ArrayList by applying simple logic as shown here.
Java Array Example
In this example, we have an int array number
. The size of the array is 5, which means it can store upto 5 elements. We are demonstrating how the elements are assigned to an array and later we printed all the elements of the array using for loop.
public class JavaExample { public static void main(String args[]) { // An int array that can store upto 5 elements // Name of the array is "number" int number[]=new int[5]; //storing values in the array number[0]=100; //first element number[1]=101; //second element number[2]=200; //third element number[3]=250; //fourth element number[4]=10; //fifth element //printing the elements of array "number" for(int i=0;i<number.length;i++) { System.out.println("number["+i+"]: "+number[i]); } } }
Output:
number[0]: 100 number[1]: 101 number[2]: 200 number[3]: 250 number[4]: 10
Java ArrayList Example
In this example, we have defined an ArrayList of strings. Unlike array, here you do not need to specify the size, you can add and remove elements and the arraylist will grow and shrink dynamically based on the need. You can however find the length of the arraylist to know the current number of elements in the ArrayList.
We have added four string elements to the arraylist al
and displaying the elements at the end of the program.
import java.util.*; public class JavaExample { public static void main(String args[]) { //Creating an ArrayList of strings //Name of the arraylist is "al" ArrayList<String> al = new ArrayList<String>(); //adding elements to the arraylist using add() al.add("BeginnersBook"); al.add("Website"); al.add("Chaitanya"); al.add("Tutorials"); //printing the elements of the arraylist for(String str:al) { System.out.println(str); } //You can also print the complete arraylist like this System.out.println(al); } }
Output:
BeginnersBook Website Chaitanya Tutorials [BeginnersBook, Website, Chaitanya, Tutorials]
Recommended Articles:
1. How to make an ArrayList read only
2. Convert Array to ArrayList
3. Convert ArrayList to Array