<aside>
📦
L2.2 — Collections and Iterators covers the limitations of arrays, introduces ArrayList as a dynamic alternative (declaration, insertion, access, removal), explains generics for type safety, autoboxing/unboxing for primitives, and the Iterator pattern for decoupled collection traversal.
</aside>
📋 At a Glance
- [ ] Understand the four main problems with arrays (fixed size, painful insertion, index-only access, bounds exceptions)
- [ ] Know that ArrayList is a Java library class (not part of the language itself)
- [ ] Compare array vs ArrayList: declaration, insertion, access, removal
- [ ] Understand how ArrayLists are stored in memory vs arrays (linked nodes vs contiguous block)
- [ ] Use generics (
ArrayList<Type>) to enforce type safety at compile time
- [ ] Understand why
instanceof + typecasting is a last resort
- [ ] Know that collections can only store objects, not primitives
- [ ] Explain autoboxing (primitive to wrapper) and unboxing (wrapper to primitive)
- [ ] Use Iterator objects with
hasNext() and next() to traverse collections
- [ ] Explain why iterators are useful (neater code, decoupling from collection)
1. Problems with Arrays
Arrays have four key limitations:
- Fixed size — once created, the size cannot change
- Painful insertion — adding new elements is difficult if you don't know how many exist already
- Index-only access — you must use numeric indices to interact with elements
- ArrayIndexOutOfBoundsException — accessing a nonexistent index causes a runtime error