ArrayLists

ArrayLists allow you to store a variable amount of information. This makes them more versatile, albeit slower, than normal arrays.

Technically you can have things of different types because of how they work, but it’s not recommended and may lead to unexpected behavior.

Usage

ArrayList<ArrType> array = new ArrayList<>();
array.add(/* something */);

Common Methods

  • .size() returns an int of the amount of elements
  • .add(element) adds some element
    • .add(index, element) adds an element at some specific index
  • .remove(index) removes an element at a specific index
  • .get(index) returns the element at an index
  • .set(index, element) replaces a value at some index with the element given

Notes

  • You can only store reference types in ArrayLists
    • You need to use wrapper classes for primitives

ArrayList Visualizer