Module lessons (2/2)
Collections and ArrayList
Dynamic lists with ArrayList<E>, wrapper classes vs primitives, List.of() and traps of .remove() in iteration.
What you'll learn in this lesson
- Replace hard arrays with
ArrayList<E>leveraging autoscaling and generics. - Understand autoboxing/unboxing and why generic collections accept wrapper classes (
Integer) and never primitives (int). - Avoid the two most common traps:
ConcurrentModificationExceptionbeing removed and the ambiguousremove(int index)vsremove(Object)overload.
Overview
Static arrays have a fixed length decided at the beginning. When the number of elements grows or varies at runtime, Java offers ArrayList<E>, a dynamic list backed by array that grows automatically.
Program preferably towards the List<E> interface using the diamond operator <> to avoid repeating the type on the right:
Sections covered in this lesson
- 11. Create, Populate and Read a
ArrayList - 22. Wrapper, Autoboxing and
IntegerCache types - 33.
List.of(...)Immutable vsnew ArrayList<>(...) - 44. Two Classic Traps of
ArrayList:.remove(int)andfor-each - 5Try it yourself
Unlock "Collections and ArrayList" and the full Course Java curriculum
Dynamic lists with `ArrayList<E>`, wrapper classes vs primitives, `List.of()` and traps of `.remove()` in iteration.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course Java plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.