Java Array vs ArrayList

Understand the concepts of the array and ArrayList and the differences between them.

Natasha Ferguson
4 min readNov 14, 2022

Array

Array Introduction

An array is a type of data structure of fixed length that allows you to store an ordered sequence of values that are all of the same type. For example, you can create an array of characters, numbers, etc. It works for all primitive types and even objects like String. An array has a contiguous memory location. Once an array is created, we cannot change its size. Here’s a general example of an array reference variable declaration and array allocation.

dataType[] arrayName = new dataType[numElements];

When we try to add more than its size, it throws ArrayIndexOutOfBoundsException.

Array Declarations

The array declaration uses [ ] symbols after the data type to indicate that the variable is an array reference. An array reference variable can refer to arrays of various sizes. The new keyword creates space in memory to store the array with a specific number of elements. The array reference variable is assigned to refer to that newly allocated array. The following example declares an array reference variable gameScores, allocates an array of 4 integers, and assigns gameScores to refer to the allocated array.

int[] gameScores = new int[4];

An array’s elements are automatically initialized to default values when using the new keyword to initialize the array reference. The default value for elements of integer and floating-point data types is zero, and the default value for boolean elements is false.

You can also declare an array reference variable without allocating the array at that time and later assign the variable with an allocated array.

int[] gameScores;

gameScores = new int[4];

Array Initialization With Non-Default Values

You can initialize an array’s elements with non-default values by specifying the initial values in braces {} separated by commas.

int[] myArray = {5, 7, 11}; 

The above example creates an array of three integer elements with values 5, 7, and 11. Such array initialization does not require the use of the new keyword, because the array’s size is automatically set to the number of elements within the braces. For larger arrays, initialization may be done by first defining the array, and then using a loop to assign array elements.

The Array elements can be accessed by using their indices that start at zero.

Int[ ] intArray = new int [ ] {2};
intArray [0] = 1;
intArray [1] = 2;

ArrayList

ArrayList Introduction

An ArrayList is a resizable ordered list of reference type items that comes with Java. It is a class belonging to the Java Collection Framework. It provides us with dynamic arrays, it handles resizing automatically. As elements get added to an ArrayList, its memory capacity grows automatically. And that’s the cool thing about ArrayLists.

The statement import java.util.ArrayList; enables the use of an ArrayList. We can create an instance of ArrayList by using the following statement:

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

The ArrayList list size can grow to contain the desired elements. ArrayList does not support primitive types like int, but rather reference types like Integer. A common error among beginners is to declare an ArrayList of a primitive type like int, as in ArrayList<int> myVals, yielding a compilation error: "unexpected type, found : int, required: reference."

Example: Create an ArrayList object called cars that will store strings:

import java.util.ArrayList; 

ArrayList<String> cars = new ArrayList<String>();

To add elements to the ArrayList, use the add() method. To access an element of the ArrayList use the get() method.

cars.add("Tesla");
cars.add("BMW");
cars.add("Kia");
cars.get(0);

The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one). While elements can be added and removed from an ArrayList whenever you want.

Similarities

  • Array and ArrayList both are used for storing elements.
  • Array and ArrayList both can store null values.
  • Both processes take place in constant time.
  • They can have duplicate values.
  • Array and ArrayList fail to guarantee ordered elements.

Key Differences

Conclusion

The major difference between Arrays and ArrayList is the static nature of Array and the dynamic nature of ArrayList. Once created you cannot alter the size of an Array, whereas an ArrayList can re-size itself as and when required. Another important difference is that an Array is a basic functionality provided by Java. On the other side, ArrayList is a part of the collection framework in Java. We can access array members using the square bracket, in which we can specify the index. While to access ArrayList members and modify them, there is a set of methods.

Despite the fact that they are different, they are nevertheless comparable in other respects. Both of these data structures in Java are index-based and let you store objects. Additionally, both of these data structures permit null values and duplicates. If you know the size of the objects ahead of time, you may want to use an array; however, if you are unsure about the size, you should use the ArrayList instead.

Check out my Java Cheatsheet for a quick core Java refresher.

--

--