Difference between Array vs ArrayList in Java

In this post, we will discuss one important and frequently asked Java interview question that is the difference between Array and ArrayList in Java.

Difference of Array and ArrayList

Here is the summary of the difference between Array and ArrayList in Java:

Let's understand each difference in detail.

1. An Array is static in nature i.e of fixed length. The size of an array is fixed and cannot be changed after the array has been created. ArrayList is dynamic in nature. If you add elements to an ArrayList, it will automatically increase its size.

2. 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 the type of Array. ArrayList allows you to use Generics to ensure type safety.

3. An Array can contain both primitive and Object data types. ArrayList does not contain primitive data types (but contains primitive Wrapper classes). It only contains object entries.

4. Java provides add() method to insert an element into ArrayList and we can use the assignment operator to store elements into Array.

ArrayList.add() method:
        List<String> animals = new ArrayList<>();
        // Adding new elements to the ArrayList
        animals.add("Lion");
        animals.add("Tiger");
Assignment operator to store elements into Array:
// initialize primitive one dimensional array
int[] anArray = new int[5];

anArray[0] = 10; // initialize first element
anArray[1] = 20; // initialize second element
anArray[2] = 30; // and so forth
5. When creating an Array object, it's mandatory to provide the size of Array but when creating an ArrayList object, no need to pass the size, if the size is not passed then ArrayList is internally initialized with a default size of 10.

6. Length of the ArrayList is provided by the size() method while Each array object has the length variable which returns the length of the array.

Example 1: find the length of the ArrayList using the size() method:
import java.util.ArrayList;
import java.util.List;

public class Main {
    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.size());
    }
}

Output:

4
Example 2: find the length of an Array using the length variable:

public class Main {
    public static void main(String[] args) {
        int[] array = { 100, 200, 300, 400, 500 };
        System.out.println("Array length -> " + array.length);
    }
}
Output:

Array length -> 5

7. The Array can be multidimensional but ArrayList is always single-dimensional.
8. Use Array, when you know that the size is fixed and use ArrayList when you don't know about the size of elements.
9. The performance of Array and ArrayList depends on the operation you are performing :

resize() operation: Automatic resize of ArrayList will slow down the performance as it will use a temporary array to copy elements from the old array to the new array.

add() or get() operation: adding an element or retrieving an element from the array or ArrayList object has almost the same performance, as for ArrayList object these operations run in constant time.

Array and ArrayList Example

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

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments

  1. how can you say that arrays is unordered

    Unordered : Both does not guarantee ordered elements.

    ReplyDelete
    Replies
    1. You are correct, Array and ArrayList preserve the insertion order. This point may be removed.

      Delete

Post a Comment