What is an ArrayList?
An arraylist is an ordered collection of items - similar to an array, and is a class in the java.util
package. It can be used to store objects
of the same datatype like an array, however, it cannot hold primitive datatypes. It also does not have a fixed capacity, and elements can be freely
added or removed from the arraylist. As shown below, an arraylist can be initialized in 2 ways, either with or without an initial capacity.
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList arr1 = new ArrayList(); // Default initial capacity set to 10
ArrayList arr2 = new ArrayList(5); // Initial capacity set to 5
ArrayList arr3 = new ArrayList(Arrays.asList(1, 2, 3)); // arr3 initialized to [1, 2, 3]
}
}
Though an initial capacity technically does not need to be specified since an arraylist will dynamically resize itself when it reaches maximum capacity, resizing an arraylist would have a cost of O(n) (click here to learn about time complexity) since all previously added elements would need to be copied to a new arraylist, while adding a new element to an arraylist when resizing isn't required happens in O(1) time. As a result, if the size of the arraylist is already known, it is more time efficient to set the size of the arraylist beforehand.
Why use ArrayLists?
ArrayLists are useful in situations where the array needs to be dynamically resized (e.g. situations where the size of the array required is not known). ArrayLists also have API methods, which allow for more features (e.g.ArrayList.contains()
allows one to easily check if a specified element exists in a the given ArrayList).
Overall, ArrayLists are slower than arrays, so if speed is a key factor, using arrays is preferable. However, ArrayLists can grow and shrink when needed and also have a variety
of methods which allow for more functionality, so if ease of use and flexibility are key factors, an arraylist would be preferred.
Adding and Removing Elements in an ArrayList
Elements in an arraylist can accessed by their index with the arraylist.get() and arraylist.set() methods. For example,System.out.print(arr.get(2));
prints the element at
index 2 in arraylist arr
and arr.set(2, 3);
sets the element at index 2 of arr
to 3
, given that arr
is an arraylist of
Integers
. Elements can also be added to arraylists via the arraylist.add() and arraylist.remove() methods. For example, arr.add(3)
will add 3 to the end of
arraylist arr
, arr.add(3, 2)
will put 2 at index 3 of arr
and shift all previous elements up, and arr.remove(2)
will remove the third
element in arr
. Trying to access an element outside of an array's range will throw an IndexOutOfBoundsException
.
Shown below is a demonstration of this functionality.
public class AccessingElements {
public static void main(String[] args) {
ArrayList arr = new ArrayList(Arrays.asList(1,2,3,4,5)); // arr initialized to [1,2,3,4,5]
// Get and set methods
System.out.println(arr.get(2)); // 3
arr.set(4, 10); // Sets last element in arr to 10
System.out.println(arr); // [1,2,3,4,10]
// Adding and removing elements
arr.remove(2); // Removes element at index 2
System.out.println(arr); // [1,2,4,10]
arr.add(20); //Adds 20 to the end of arr
System.out.println(arr); // [1,2,4,10,20]
arr.add(2, 3); // Adds 3 in position 2 in arr, moves elements previously at 2 and above up 1 position
System.out.println(arr); // [1,2,3,4,10,20]
}
}