Learn how to create and use ArrayLists in your programs!
A common way to create a list of values or elements in Java is by using arrays to store data:
One of the limitations of using arrays to store data is the fact that they have a fixed number of items that can be added or changed. This can pose problems if a dataset changes in size frequently, like the number of current Avengers!
Elements can only be added to an array by creating a new array of a different size and copying the data from the initial array to the new one:
This process is tedious, and requires a high degree of difficulty, especially if the list being used is going to change in size often.
A more convenient way to create adjustable arrays is by using the ArrayList class.
ArrayLists are similar to arrays, except that they are a mutable list of object references, meaning that the size of the list can be adjusted freely. Creating an ArrayList is as simple as initializing a new ArrayList in a program:
In order to use the ArrayList class, the ArrayList class needs to be imported from the java util package. This can be done by writing import java.util.ArrayList
at the top of the class file.
ArrayList objects are created in the same fashion as other object classes. The primary difference with ArrayLists is that the element type of the ArrayList must be specified using arrows (<>). In this example, E represents the data type that will be used in the ArrayList. This can be replaced by an object data type:
The declaration type for an ArrayList must match the initialization type. In this case, both types reference the String
data type.
When this line of code is executed, the ArrayList avengers
will store an empty ArrayList with no index values. Unlike arrays, the number of items that will be stored in an ArrayList does not need to be specified.
Objects can be added to an ArrayList using the add
method. The add method has several method signatures:
The single parameter add
method will add an element to the end of the ArrayList:
Since the ArrayList starts with no values, the call to add
will add the element 5 to the 0th index of the ArrayList. If the call list.add(8)
were added to this code segment, the element 8 would be added to the 1st index position. Like arrays, ArrayLists begin at index 0. The add
method returns true
if the element was successfully added to the ArrayList.
The second add
method signature will add an element at a specified index:
The specified index must be between the value 0, and the current size of the ArrayList. To determine the size of the ArrayList, the method size
can be used to return the current length of an ArrayList:
Notice that the size of the ArrayList increases when a new value is added to the list. When add(int index, E obj)
is called, the list items from index
- size()
shift up one index to allow the new value to slot in to the correct index position.
Elements in an ArrayList can be accessed using the get(int index)
method:
The method get
returns an element at the specified index. In this case, the value stored at index 1 (3), is being stored in the variable val
, and printed to the console.
Individual elements in an ArrayList can be changed using the set(int index, E obj)
method:
In this example, the value at index 2 is changed from the value 10 to the value 34. It’s important to note that the values can only be changed to another value of the same data type.
The set
method also returns the value that is being replaced to the existing program. The value that has been replaced can then be stored in a variable of its own:
Just as elements can be added, they can be removed from an ArrayList using the remove(int index)
method.
Like the set
method, the remove
method also returns the value that is being removed from the specified index. In the program above, notice that the size of the ArrayList decreases by one when the element is removed from the ArrayList. All values at the index higher will move down one index once an element is removed from an ArrayList.
A coffee shop stores a list of their available baked goods in an ArrayList. A recent spending spree by a catering company has left them with little of their usual offerings, so they are scrambling to create a new list of baked goods to offer.
Using your newfound knowledge in creating ArrayLists, help this coffee shop change their baked goods list by:
Removing the first and the last item in the ArrayList.
Adding coffee cake to the end of the ArrayList
Adding scones at index 1.
Moving muffins to the middle of the list (Use size()
to help you figure out the middle).