List (Interface)
- เป็น Interface (ส่วนหนึ่งของ Collection Framework)
- extends Collection
- ไม่สามารถ instantiate ได้โดยตรง
- ใช้สร้างรายการ elements ที่มีลำดับ (ordered) และเข้าถึงได้ด้วย index
- อนุญาต duplicate elements
ArrayList (Class)
- เป็น Class implement List
- extends AbstractList
- สร้าง instance ได้
- เป็น Dynamic Array (ขยายขนาดอัตโนมัติ)
- เก็บ Objects (ไม่ใช่ primitives)
- Random access ได้ O(1) ผ่าน index
สรุป
| คุณสมบัติ | List | ArrayList |
|---|---|---|
| ประเภท | Interface | Class |
| Instantiate | ไม่ได้ | ได้ |
| Implementation | - | Dynamic Array |
| Extends | Collection | AbstractList |
| ใช้งาน | ประกาศตัวแปร (List<T> list = new ArrayList<>()) | Implementation จริง |
Best Practice
เขียนโค้ดตาม Interface: List เพื่อเปลี่ยน Implementation ได้ง่าย (เช่น LinkedList)