Before anything, this is the import you need:
import java.util.ArrayList;
And this is how you create one:
ArrayList<String> list = new ArrayList<>();
Simple. Now let's get into the methods.
The Methods
add(element)
Adds an element to the end of the list.
list.add("Apple");
add(index, element)
Adds an element at a specific position. Shifts everything else to the right.
list.add(1, "Banana");
get(index)
Returns the element at that index.
String fruit = list.get(0); // Apple
set(index, element)
Replaces the element at that index with a new one.
list.set(0, "Mango"); // Apple becomes Mango
remove(index)
Removes the element at that index.
list.remove(1); // removes Banana
remove(object)
Removes the first occurrence of that specific value.
list.remove("Mango");
Heads up — if your list is
ArrayList<Integer>, Java gets confused betweenremove(index)andremove(object). Wrap the value:list.remove(Integer.valueOf(5)).
size()
Returns how many elements are in the list.
int count = list.size();
isEmpty()
Returns true if the list has no elements.
boolean empty = list.isEmpty();
contains(object)
Returns true if the list has that value.
boolean has = list.contains("Apple");
indexOf(object)
Returns the index of the first occurrence. Returns -1 if not found.
int pos = list.indexOf("Apple");
lastIndexOf(object)
Returns the index of the last occurrence. Useful when duplicates exist.
int lastPos = list.lastIndexOf("Apple");
clear()
Removes everything. List becomes empty.
list.clear();
addAll(collection)
Adds all elements from another collection into this list.
ArrayList<String> more = new ArrayList<>();
more.add("Grapes");
more.add("Papaya");
list.addAll(more);
addAll(index, collection)
Same as above but inserts at a specific position.
list.addAll(1, more);
removeAll(collection)
Removes all elements that exist in the given collection.
list.removeAll(more);
retainAll(collection)
Keeps only the elements that exist in the given collection. Removes everything else.
list.retainAll(more);
containsAll(collection)
Returns true if this list contains every element in the given collection.
boolean hasAll = list.containsAll(more);
toArray()
Converts the ArrayList into a plain array.
Object[] arr = list.toArray();
subList(fromIndex, toIndex)
Returns a portion of the list. fromIndex is included, toIndex is not.
List<String> part = list.subList(0, 2); // index 0 and 1 only
Collections.sort(list)
Not an ArrayList method exactly, but used all the time. Sorts the list in ascending order.
import java.util.Collections;
Collections.sort(list);
Collections.reverse(list)
Reverses the order of elements.
Collections.reverse(list);
Quick Reference Table
| Method | What it does |
|---|---|
add(element) |
Adds to end |
add(index, element) |
Inserts at position |
get(index) |
Returns element at index |
set(index, element) |
Replaces element at index |
remove(index) |
Removes by position |
remove(object) |
Removes by value |
size() |
Returns total count |
isEmpty() |
Checks if list is empty |
contains(object) |
Checks if value exists |
indexOf(object) |
First position of value |
lastIndexOf(object) |
Last position of value |
clear() |
Removes everything |
addAll(collection) |
Merges another list |
addAll(index, collection) |
Merges at a position |
removeAll(collection) |
Removes matching elements |
retainAll(collection) |
Keeps only matching elements |
containsAll(collection) |
Checks if all exist |
toArray() |
Converts to plain array |
subList(from, to) |
Returns a slice of the list |
Collections.sort() |
Sorts ascending |
Collections.reverse() |
Reverses the list |