Difference between Array and ArrayList in Java
This post will discuss the difference between an array and ArrayList in Java.
An array is a fixed-length container that holds multiple elements of the same type. However, arrays have some limitations, such as requiring a predefined size, not allowing dynamic resizing, and not supporting generics. To overcome these problems, Java also provides a class called ArrayList, which is a variable-length collection that can store any type of objects and can grow or shrink as needed.
1. Overview of an array
An array is a basic functionality provided by Java, which allows us to create and store a fixed number of elements of the same type in a contiguous memory location. We can access and modify the elements of an array by using their index, which starts from zero. We can also create multidimensional arrays, which are arrays of arrays. The dimension of an array is the total number of indices needed to select an element.
To create an array in Java, we need to declare its type, name, and size using the following syntax:
|
1 |
type[] arrayName = new type[size]; |
We can also initialize an array with some values using the following syntax:
|
1 |
type[] arrayName = {value1, value2, value3, …, valueN}; |
We can access and modify the elements of an array by using their index within square brackets. We can also use loops or enhanced for-loop (aka for-each loop) to iterate over the elements of an array.
2. Overview of an ArrayList
An ArrayList is a class that implements the List interface and uses a dynamically resizable array as its underlying data structure to store elements. It can store any type of objects, including wrapper classes, null values, and custom classes. An ArrayList can also grow or shrink as needed, without requiring a predefined size. To create an ArrayList in Java, we need to import the java.util package and declare its type, name, and optionally its initial capacity using the following syntax:
|
1 |
ArrayList<T> list = new `ArrayList`<T>(initialCapacity); |
We can also omit the initial capacity parameter if we don’t know how many elements we will need. We can add elements to an ArrayList by using the add() method, and can access and modify the elements of an ArrayList by using their index within the get() and set() methods. We can also use loops or enhanced for loop to iterate over the elements of an ArrayList.
3. Differences between arrays and ArrayLists
Arrays and ArrayLists are both useful data structures in Java, but they also have some differences and trade-offs that make them suitable for different scenarios:
- Arrays are fixed-size data structures, while ArrayLists are variable-size collections. This means that we cannot change the length of an array once created, but we can add or remove elements from an
ArrayListas needed. This also means that arrays may waste some memory space if they are not fully utilized. - Arrays can store primitives and objects, while ArrayLists can only store objects. This means that we can create an array of any type, such as int, char, boolean, etc., but we need to use wrapper classes, such as
Integer,Character,Boolean, etc., to store primitives in anArrayList. This also means that arrays may be faster and more efficient than ArrayLists for storing primitives. - Arrays support varargs, while ArrayLists support generics. This means that we can create an array of any generic type, such as T[], and pass an array as a variable-length argument to a method, such as foo(T… args), but we cannot do the same with an
ArrayList. - Arrays only have a few built-in methods, such as length and clone, but ArrayLists have many built-in methods, such as add, remove, get, set, size, contains, indexOf, lastIndexOf, clear, isEmpty, toArray, sort, etc. Arrays are simpler and easier to use for basic operations, but ArrayLists may be more powerful and useful for complex operations.
- Arrays take
O(n)space fornnumber of elements and do not reserve any additional storage, while anArrayListreserve linearO(n)additional storage. We can calltrimToSize()method ofArrayListclass to minimize the storage of anArrayListinstance. - Arrays in Java support one-dimensional array, two-dimensional array, three-dimensional array, and so on. While an
ArrayListhas no concept of dimensions, but we can easily construct an ArrayList of ArrayLists.
4. What to use and when?
As we have seen, arrays and ArrayLists are both useful data structures in Java. Here are some general recommendations on how to choose between them:
- If you need a fast and efficient data structure that can store primitives or objects of a known and fixed size, we should use an array.
- If you need a flexible and powerful data structure that can store any type of objects of an unknown or variable size, we should use an
ArrayList.
That’s all about the differences between an array and ArrayList in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)