7.2 ArrayList Methods
ArrayLists provide flexibility and functionality that arrays do not. You can think of an ArrayList as a container that will resize as you add and remove objects from it. In order to do so, there are several helper methods that are used to alter the state of an ArrayList, and get information about its content.
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 stores 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.
Many of the methods used to access and manipulate ArrayLists are functionally similar in nature to arrays:
Feature | Arrays | ArrayLists |
---|---|---|
Initialize | int[] arr = new int[5]; | ArrayList array = new ArrayList(); |
Get Value | arr[index] | array.get(index) |
Get Size | arr.length | array.size() |
Set Value | arr[index] = value; | array.set(index, value) |
Feature | Arrays | ArrayLists |
---|---|---|
Remove Value | N/A | array.remove(index) |
Add Value | N/A | array.add(value) OR array.add(index, value) |
When considering which data structure to use, it’s important to determine early on if it’s necessary to add or remove values at future points in the program. If that’s the case, then ArrayLists may serve better.
-
Incorrect
Correct
No Answer was selected
Invalid Answer
What value will be printed in the console after this series of commands? Assume that the ArrayList package has been imported.
Create an ArrayList with elements that are odd numbers from 101 - 199.
Hint: Use a loop!