Javatpoint Logo
Javatpoint Logo

Difference between Array and ArrayList

In Java, array and ArrayList are the well-known data structures. An array is a basic functionality provided by Java, whereas ArrayList is a class of Java Collections framework. It belongs to java.util package.

Java Array

An array is a dynamically-created object. It serves as a container that holds the constant number of values of the same type. It has a contiguous memory location. Once an array is created, we cannot change its size. We can create an array by using the following statement:

The above statement creates an array of the specified size. When we try to add more than its size, it throws ArrayIndexOutOfBoundsException. For example:

Java ArrayList class

In Java, ArrayList is a class of Collections framework. It implements List<E>, Collection<E>, Iterable<E>, Cloneable, Serializable, and RandomAccess interfaces. It extends AbstractList<E> class.

We can create an instance of ArrayList by using the following statement:

ArrayList is internally backed by the array in Java. The resize operation in ArrayList slows down the performance as it involves new array and copying content from an old array to a new array. It calls the native implemented method System.arraycopy(sec, srcPos, dest, destPos, length) .

We cannot store primitive type in ArrayList. So, it stores only objects. It automatically converts primitive type to object. For example, we have create an ArrayList object,

The JVM converts it into Integer object through auto-boxing.

Similarities

  • Array and ArrayList both are used for storing elements.
  • Array and ArrayList both can store null values.
  • They can have duplicate values.
  • They do not preserve the order of elements.

The following table describes the key differences between array and ArrayList:

Basis Array ArrayList
Definition An array is a dynamically-created object. It serves as a container that holds the constant number of values of the same type. It has a contiguous memory location. The ArrayList is a class of Java Collections framework. It contains popular classes like Vector, HashTable, and HashMap.
Static/ Dynamic Array is static in size. ArrayList is dynamic in size.
Resizable An array is a fixed-length data structure. ArrayList is a variable-length data structure. It can be resized itself when needed.
Initialization It is mandatory to provide the size of an array while initializing it directly or indirectly. We can create an instance of ArrayList without specifying its size. Java creates ArrayList of default size.
Performance It performs fast in comparison to ArrayList because of fixed size. ArrayList is internally backed by the array in Java. The resize operation in ArrayList slows down the performance.
Primitive/ Generic type An array can store both objects and primitives type. We cannot store primitive type in ArrayList. It automatically converts primitive type to object.
Iterating Values We use for loop or for each loop to iterate over an array. We use an iterator to iterate over ArrayList.
Type-Safety We cannot use generics along with array because it is not a convertible type of array. ArrayList allows us to store only generic/ type, that's why it is type-safe.
Length Array provides a length variable which denotes the length of an array. ArrayList provides the size() method to determine the size of ArrayList.
Adding Elements We can add elements in an array by using the assignment operator. Java provides the add() method to add elements in the ArrayList.
Single/ Multi-Dimensional Array can be multi-dimensional. ArrayList is always single-dimensional.

Example of Array in Java

In the following example, we have simply created an array of length four.

Output:

12
2
15
67

Example of ArrayList in Java

In the following example, we have created an instance of ArrayList and performing iteration over the ArrayList.

Output:

12.4
34.6
56.8
78.9

Next TopicJava Tutorial




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA