ArrayList in Java
ArrayList, from java.util, is a resizable array. Unlike a plain array, it grows automatically as elements are added, with no fixed size decided up front. Internally, it still stores elements in a real array behind the scenes, and it manages growing that backing array transparently whenever more space is needed.
Reading or writing by index (get, set) is fast regardless of the list's size, since it's just indexing into the backing array directly. But inserting or removing from anywhere except the very end requires shifting every subsequent element over by one position, which gets slower as the list grows.
Creating one
new ArrayList<Integer>() starts empty; its type parameter (Integer here) fixes what kind of element it can hold.
Adding elements
add(value) appends to the end; add(index, value) inserts at a specific position, shifting later elements over.
Reading and writing by index
get(index) and set(index, value) both run in constant time, just like indexing a plain array.
Removing elements
remove(index) or remove(Object) both shift every element after the removed one back by one position.
Automatic resizing
Once the backing array fills up, ArrayList transparently allocates a larger one and copies existing elements across, all without the caller doing anything.
import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList<String> names = new ArrayList<>(); names.add("Ana"); names.add("Bo"); names.add(0, "Zed"); System.out.println(names); System.out.println("Size: " + names.size()); }}Random access and appending are fast because they touch the backing array directly. Inserting or removing anywhere but the end costs a shift proportional to how many elements come after it.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.