Commit a6091bd
committed
feat(ArrayListCapacityReflection): demonstrate ArrayList capacity via reflection
What
- Added ArrayListCapacityReflection class.
- Created an ArrayList<Integer> with initial capacity 11.
- Used reflection to access the private backing array `elementData`.
- Printed internal capacity at different stages:
- After filling with 11 elements.
- After adding one more (triggering resize).
- After removing several elements.
- After calling trimToSize().
Why
- Show how ArrayList manages internal capacity separately from size.
- Reveal that capacity expands automatically on growth but does not shrink automatically on removal.
- Demonstrates `trimToSize()` as the only way to force capacity reduction.
- Provides deeper insight into ArrayList’s underlying array mechanics.
How
- Constructed list = new ArrayList<>(11).
- Filled with 11 items → capacity remains 11.
- Added one extra element:
- Internally triggers resize (capacity grows, typically by ~50%).
- Removed 8 elements:
- Size shrinks but capacity stays the same.
- Called trimToSize():
- Backing array shrinks to match current size.
- Used reflection:
- Field elementData = ArrayList.class.getDeclaredField("elementData").
- field.setAccessible(true).
- (Object[]) field.get(list) yields the underlying array for measuring capacity.
Logic
- Inputs: integer elements.
- Outputs: printed internal capacity values.
- Flow:
1. Create list with capacity 11.
2. Add 11 → capacity stays 11.
3. Add 12th → resize triggers, capacity increases.
4. Remove 8 → capacity unchanged.
5. Call trimToSize() → capacity shrinks to match remaining size.
- Edge cases:
- Reflection is unsafe and can break with JVM updates.
- `elementData` is an implementation detail, not part of the API contract.
- Complexity: O(1) average for add/remove, O(n) for resizing/trimToSize.
- Concurrency: ArrayList is not thread-safe.
- Error handling: None, demo assumes success.
Real-life applications
- Useful for teaching memory behavior of ArrayList.
- Debugging performance issues related to capacity vs size.
- Demonstrates when to use trimToSize() in memory-sensitive contexts.
Notes
- Reflection-based inspection is for educational/demo use only, not production.
- In production, rely on documented APIs: ensureCapacity(int) and trimToSize().
- Capacity growth strategy: new capacity = oldCapacity + (oldCapacity >> 1) (i.e., 1.5x growth).
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent fc5fe72 commit a6091bd
File tree
1 file changed
+34
-0
lines changed- Section 25 Collections Frameworks/List Interface/ArrayList/Reflections/src
1 file changed
+34
-0
lines changedLines changed: 34 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
0 commit comments