Java Programming Techniques and APIs

Java Array and ArrayList: Differences

Java Array and ArrayList: Differences

Introduction

An array is a linear data structure that is used to store elements in a contiguous memory location. The ArrayList is a class in Java that is similar to a dynamic array and belongs to the collection framework. The ArrayList class works almost similar to the vector class in C++.

Ways to create an array in Java

1. Static array or Simple fixed-sized arrays.

2. Dynamically sized arrays

We can declare a static array and initialize it later or we can initialize an array at the time of declaration. Let us consider the following program that illustrates how we can declare a static and dynamic array:

Source Code

class HelloWorld {
    public static void main(String[] args) {
   
        // Create an array of size 5
        int[] array1 = new int[5];
       
        // Start filling values
        array1[0] = 10;
        array1[1] = 20;
        array1[2] = 20;
        array1[3] = 30;
        array1[4] = 40;
       
        // Printing the first element of array
        System.out.println(array1[0]);
   
        // Create an array of size 5
        int[] array2 = {100, 200, 300, 400, 500};
       
        // Printing the first element of array
        System.out.println(array2[0]);
    }
}

Output:

java -cp /tmp/s8IvllDeLH HelloWorld
10
100

Output Description:

As you can see in the output, the first element of both the arrays have been displayed.

write your code here: Coding Playground

Creating an ArrayList

Java provides us the ArrayList class using which we can store elements just like a dynamic array. The ArrayList is a part of the collection framework in Java. We can access the elements of an ArrayList using the [] operator. The ArrayList class also provides methods to access elements.

We can declare an ArrayList in Java using the following syntax:

ArrayList<data_type> arraylist = new ArrayList<data_type>();

Source Code

import java.util.ArrayList;
class HelloWorld {
    public static void main(String[] args) {
   
        // Create an arraylist
        ArrayList<Integer> arraylist = new ArrayList<Integer>(5);

        // Adding elements to ArrayList
        // using add() method
        arraylist.add(10);
        arraylist.add(20);
        arraylist.add(30);
        arraylist.add(40);
        arraylist.add(50);

        // Display the arraylist elements
        System.out.println(arraylist);
    }
}

Output:

java -cp /tmp/s8IvllDeLH HelloWorld
[10, 20, 30, 40, 50]

Output Description:

As you can see in the output, ArrayList elements have been displayed.

write your code here: Coding Playground

Differences Table

The differences between an ArrayList and an array are the following:

Array

ArrayList

An array can be single-dimensional as well as multidimensional.

An ArrayList can be single-dimensional only.

Operations on an array can be performed fast as compared to an ArrayList.

An ArrayList is slower than compared to Array.

An array is static and thus has a fixed length.

An ArrayList is dynamic in nature and therefore can be increased or decreased accordingly.

There are no other methods except the [] operator to add elements.

We have add() method to add elements in an ArrayList.

The length keyword is used to retrieve the size of an array.

The size() method is used to retrieve the size of an ArrayList.

Conclusion

In this tutorial, we discussed how we can create an array and ArrayList in Java. We also highlighted differences between array and ArrayList. We believe this tutorial has helped you to improve your Java knowledge.