Home | Unit 1 | Unit 2 | Unit 3 | Unit 4 | Unit 5 | Unit 6 | Unit 7 | Unit 8 | Unit 9 | Reflection |
Unit 7
My notes on unit 7.
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