1. ArrayList
class
Today we will explore the ArrayList
class. This is the first of several classes known as collections. In Java, collections are such a broad and useful topic that an entire CodeGym quest is devoted to them.
To fully understand how collections are structured and all their nuances, you need to first learn OOP, interfaces, inheritance, the basics of multithreading, and much more.
So today we'll just get acquainted with the simplest kind of collection. But at a deep enough level so that you understand how to use it and how it works. Now then, meet the ArrayList
collection.
The backstory
I'll start with a little background. Programmers really didn't like one aspect of arrays: the fact that their size cannot be changed. What if you need to store three more elements in an array, but there is only one empty cell?
The only solution to the space limitations of an array was to create a very large array in order to accommodate all the elements you might need to store. But this was usually a waste of memory. If an array usually contained two or three elements but there was even a tiny chance that it would need to store 100 of them, then it was necessary to create an array with the capacity to store 100.
So what did programmers come up with? They wrote the ArrayList
class, which did the same work as the Array
class, but was resizable.
ArrayList class
The name of the ArrayList
class is formed from two words: Array + List. Array
is an array and List
is a list.
Each ArrayList
object contains an ordinary array of elements. When you read elements from an ArrayList
, the object retrieves them from its internal array. When you write elements, it writes them to the internal array.
The ArrayList class lacks all the disadvantages that arrays have. It knows how to:
- Store elements of a specific type
- Dynamically resize the list
- Add elements to the end of the list
- Insert elements at the beginning or middle of the list
- Remove elements from anywhere in the list
For more details, see below:
2. Creating an ArrayList
object
To create an ArrayList
object, you need to write code like this:
ArrayList<TypeParameter> name = new ArrayList<TypeParameter>();
Where ArrayList
is the collection type/class, TypeParameter
is the type of the elements stored in the ArrayList
collection, and name
is the name of an ArrayList<TypeParameter>
variable.
The name
variable has a generic type. It consists of two types: the type of the collection is indicated first, and then angle brackets are used to indicate the type of the elements stored in the collection.
Examples:
Code | Description |
---|---|
|
List of integers |
|
List of strings |
|
List of real numbers |
Unlike arrays, collections cannot store primitive types, only reference types. So if you need a collection of int
s, use the Integer
wrapper class instead.
3. Operations with an ArrayList
Initially, the length of the newly created list is zero, since it contains 0 elements. If you add one element to the list, its length increases by 1. If you remove the added element, the length decreases back to zero.
The following table can teach you more about the methods of the ArrayList
class:
Methods | Description |
---|---|
|
Adds the passed element to the list |
|
Adds an element to a specific location in the list. |
|
Returns the element whose index is index |
|
Assigns value to the element whose index is index |
|
Removes the element whose index is index . Returns the removed element. |
|
Removes the element that you pass to the method. If there is more than one such element, the first one will be removed. |
|
Clears the list, i.e. removes all elements from the list. |
|
Checks whether the list contains value . |
|
Checks whether the list is empty or not. In other words, whether the length of the list is zero. |
|
Returns the size of the list, i.e. the number of elements in the list. |
|
Returns an array containing the elements of the list. You need to pass the array to the method. |
These methods let you do almost anything you might want to with the list: swap elements, add elements, and remove elements. You can clear the list with a single command, or even convert the list to an array.
4. Comparison of ArrayList
and Array
I don't think we can avoid comparing ArrayList
and an array.
There are only 4 actions you can perform with arrays:
- Create an array
- Get an element by index
- Set an element by index
- Get the length of the array
Here are these operations as they apply to an array and an ArrayList
:
Array | ArrayList |
---|---|
|
|
|
|
|
|
|
|
Let's compare how an ArrayList
works versus how an array works. For example, let's implement this task: "read 10 strings from the keyboard and display them on the screen in reverse order"
Using Array | Using ArrayList |
---|---|
|
|
The analogy is clear. Everything is somehow shorter and clearer for arrays. But ArrayList
isn't difficult either: to get an element, we use the get()
method; to change an element, the set()
method; to get the length of the list, the size()
method.
So why do programmers use the ArrayList
class?
The whole point, of course, is all the other methods that ordinary arrays don't have:
- Add an element to the list
- Add an element to the middle of the list
- Find an element in the list
- Removing an element from a list
GO TO FULL VERSION