Skip to main content
Module 4 · Lesson 2 of 210/22 in the course~15 min
Module lessons (2/2)

Collections and ArrayList

Lesson preview5 hands-on exercises readyCourse Java · Module 4 — Interfaces and Dynamic Collections

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: ConcurrentModificationException being removed and the ambiguous remove(int index) vs remove(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

  1. 11. Create, Populate and Read a ArrayList
  2. 22. Wrapper, Autoboxing and Integer Cache types
  3. 33. List.of(...) Immutable vs new ArrayList<>(...)
  4. 44. Two Classic Traps of ArrayList: .remove(int) and for-each
  5. 5Try it yourself
Pro Lesson · Module 4 — Interfaces and Dynamic Collections

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.