Resizable arrays
Many programs need to use arrays to store and process elements of the same type. But an array suffers from a significant drawback - it has a fixed size. Thus, you cannot add a new item when your array is already full.
Fortunately, the Java Class Library provides a widely-used collection named ArrayList
. It represents a resizable array of objects of a specified type. It can dynamically grow and shrinks after addition and removal of elements. This behavior is very useful if you do not know the size of the array in advance or you just need one that can change sizes over the lifetime of a program.
Like a standard array, ArrayList
allows you to get the current number of elements (size) as well as access the elements by their indexes.
There is only one restriction - it cannot store primitive types because it is a generic class, but it can store any reference types, including String
's, Integer
's, other ArrayList
's, and custom classes. The element of the class can be even null
.
To use the class by its short name, make the following import:
import java.util.ArrayList;
Creating an instance of ArrayList
The simplest way to create an instance of ArrayList
is to use a no-argument constructor.
ArrayList<String> firstArrayList = new ArrayList<>();
The created list is empty, but its initial capacity is 10 (by default).
We can also specify the initial capacity of a list:
ArrayList<String> secondArrayList = new ArrayList<>(50);
The secondArrayList
is empty, but its initial capacity is set to 50.
You can construct an ArrayList which consists of elements of another collection:
ArrayList<String> thirdArrayList = new ArrayList<>(secondArrayList);
Regardless of how you create an instance of ArrayList
, its size will dynamically change. In this lesson, we will create a list only with the default capacity like firstArrayList
.
Basic methods of ArrayList
The collection has a set of convenient methods which emulate and extend the functionality of standard arrays.
int size()
return the number of elements of the list;Object get(int index)
returns the object of the list which is present at the specified index;add(Object o)
adds a passed element to the last position of the collection;add(int index, Object o)
adds a passed element to the specified position of the collection;set(int index, Object o)
replaces the element present at the specified index with the object;remove(Object o)
removes the object from the array;remove(int index)
removes element from a given index;clear()
removes all elements from the collection.
Note, indexes of elements start with 0 just like for standard arrays.
The following example fully demonstrates the listen methods in practice.
ArrayList<String> names = new ArrayList<>(); // empty collection of strings
System.out.println(names.size()); // 0
names.add("Justin"); // now: [Justin]
names.add("Helen"); // now: [Justin, Helen]
names.add("Joshua"); // now: [Justin, Helen, Joshua]
System.out.println(names); // [Justin, Helen, Joshua]System.out.println(names.size()); // 3
System.out.println(names.get(0)); // the first element - "Justin"
System.out.println(names.get(2)); // the last element - "Joshua"
names.add(0, "Teresa"); // now: [Teresa, Justin, Helen, Joshua]
names.remove("Helen"); // now: [Teresa, Justin, Joshua]
names.clear(); // now: []
System.out.println(names.size()); // 0
The class also has a method called contains
that checks whether a list contains a value or not, and two methods (indexOf
/lastIndexOf
) which find the index of the first/last occurrence of an element (or -1 if there is no such index).
ArrayList<Integer> numbers = new ArrayList<>(); // an ArrayList of Integers, not ints
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(1);
System.out.println(numbers.contains(2)); // true
System.out.println(numbers.contains(4)); // false
System.out.println(numbers.indexOf(1)); // 0
System.out.println(numbers.lastIndexOf(1)); // 3
System.out.println(numbers.lastIndexOf(4)); // -1
Iterating over ArrayList
It is possible to iterate over elements of an instance of the class. It looks like iterating over an array. In the following example, we use for and for-each loops to add the five first power of tens in a list and then print the numbers in the standard output.
ArrayList<Long> powerOfTens = new ArrayList<>();
int count = 5;
for (int i = 0; i < count; i++) {
long power = (long) Math.pow(10, i);
powerOfTens.add(power);
}
for (Long value : powerOfTens) {
System.out.print(value + " ");
}
The code prints the following: 1 10 100 1000 10000
Conclusion
We have considered the standard ArrayList
class from the java.util package
. The class is similar to standard Java arrays. It has methods to get the size and add / access elements by indexes, but unlike an array, it can dynamically change the size. In addition, the class provides a set of useful methods to remove, find and check whether an element is presented in the array. A regular array does not have such methods as built-in.