ArrayList
ArrayList is a Java data structure designed to store multiple values. It’s an advanced array that allows you to change the number of elements and easily perform various operations on them.
Such data structures are generally called dynamic arrays. A regular array is a linear set of elements with a fixed length that can store data, typically of a single type. For example, you could have an array of numbers, strings, and so on. A dynamic array, however, does not have a fixed length. Its length changes dynamically, hence the name.
What is an ArrayList?
An ArrayList is primarily an ordered data set: the word “ordered” means that each element in the set has a specific location. This location is described by an index—a number that stores the element’s position. Indexes start at zero. The first element in the array will have index 0, the second will have index 1, and so on.
The idea behind ArrayList is expandability. When the developer increases the number of elements, the array’s length automatically increases to accommodate them all. When an element is removed, the length decreases—this makes the structure clearer, without empty spaces. You can also create an ArrayList with a preset initial capacity, but it will automatically expand as more elements are added. Furthermore, ArrayList supports many operations that aren’t possible with a regular array. For example, you can’t delete elements from a regular array—you can only reset them or overwrite them. Otherwise, the structure is similar to a regular array. Here’s why.
What is ArrayList for?
The purpose of any array, including a dynamic one, is to store structured and ordered information—a set of elements for which positioning is important. And it can be used in a huge number of ways: for storing and using data, implementing software structures and algorithms, mathematical or physical operations, and much more.
Another purpose of ArrayList is to simplify working with arrays, thanks to the abundance of special methods that we will discuss below.
How ArrayList works
Inside the data structure is a fixed-size array that is deleted and recreated with a new length when the number of objects inside the ArrayList changes. Technically, it looks like this:
- The developer gives the command to write a new element.
- Inside the ArrayList, a copy of the current array stored there is stored, but with a greater length.
- a new element is added to the desired location of the extended copy;
- The old array is deleted.
To optimize this process, the new length is by default one and a half times the old one – this way the array does not have to be “rewritten” too often.
Features of ArrayList
The List interface defines a variety of methods that make it easy to manage a dynamic array: add elements to the end or beginning, insert them anywhere in the structure, quickly retrieve data, modify the entire array at once, merge multiple collections, and much more. This doesn’t require complex algorithms or additional structures—it only takes a single line of code.
How to work with ArrayList
When creating an ArrayList, you can make it empty using the command
ArrayList<type> variable name = new ArrayList<type>();
You can also specify an existing collection in parentheses, and then a dynamic array will be created based on it and will contain its elements.
You can also specify an initial array capacity. This is done when the developer knows in advance the minimum number of elements needed in the array. This helps reduce the number of array rewrites. To specify the capacity, specify an integer in parentheses—the initial number of elements.
Then you can start working with the dynamic array: adding data with the .add(element) command, modifying it with .set(index, element), and applying various functions to elements. There are many of them, and you can find a list of them in any tutorial or documentation.
You can learn more about the Java language and its data structures in our professional course. Become a sought-after specialist with us.