SwadhyAI Notes

All Java notes

Java ArrayList Methods Reference

›Short versionOpen if you only have a minute.
  • ArrayList is a list backed by a resizable array. get and set by index are constant time.
  • contains, indexOf, and remove(Object) walk the list. They are not constant time.
  • add(element) appends. add(index, element) shifts later elements right.
  • set replaces an existing slot and returns the old value. set cannot append.
  • On ArrayList<Integer>, remove(1) removes index 1. remove(Integer.valueOf(1)) removes the value 1.
  • subList returns a view of the same list, not a copy.

ArrayList implements List with a resizing internal array. It is excellent for random access and append-heavy workloads, while middle insertions and removals require shifting elements.

Methods

APIParametersReturnsBehaviorWatch for
add(E element)Element compatible with the list generic type.boolean, usually true.Appends the element to the end and grows capacity if needed.Adding while iterating with for-each can trigger ConcurrentModificationException.
add(int index, E element)Insertion index from 0 to size(), plus element.void.Inserts and shifts existing elements right.Index must allow size() only for append-at-end insertion.
get(int index)Zero-based index.Element at that position.Reads without removing.Throws IndexOutOfBoundsException for invalid index.
set(int index, E element)Existing index and replacement element.Previous element.Replaces one slot without changing size.Cannot use set(size(), value) to append.
remove(int index) / remove(Object o)Index or object to remove.Removed element for index; boolean for object.Deletes one item and shifts later items left.For ArrayList<Integer>, remove(1) removes index 1, not value 1.
subList(int from, int to)from inclusive, to exclusive.Backed List view.Creates a view into the same list range.Structural changes to parent list can invalidate the view.

Which call fits

OptionBest forTradeoff
ArrayListFast indexed reads, appends, and compact memory layout.Middle insert/remove shifts elements.
LinkedListQueue/deque operations at both ends.Slow indexed access and higher node overhead.
HashSetUniqueness and fast contains checks without order by index.No duplicates and no positional access.

Examples

Update by index

java
ArrayList<String> tasks = new ArrayList<>();
tasks.add("draft");
tasks.add("review");

String old = tasks.set(1, "publish");
System.out.println(old);   // review
System.out.println(tasks); // [draft, publish]

Remove with a predicate

java
ArrayList<Integer> scores = new ArrayList<>(List.of(40, 75, 90));
scores.removeIf(score -> score < 50);
System.out.println(scores); // [75, 90]

ArrayList<Integer> ids holds 10, then 20, then 30. What does ids.remove(1) delete, and what call deletes the value 20?

remove(1) picks the remove(int) overload, so it deletes index 1, which is 20. The list becomes 10, 30. To delete the value 20 wherever it sits, call remove(Integer.valueOf(20)), which picks remove(Object). The same trap hits remove(1) when you meant the number 1.