Skip to content

Commit a6091bd

Browse files
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

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.lang.reflect.Field;
2+
import java.util.ArrayList;
3+
4+
public class ArrayListCapacityReflection {
5+
public static void main(String[] args) throws Exception {
6+
// Start with an initial capacity hint of 11
7+
ArrayList<Integer> list = new ArrayList<>(11);
8+
9+
// Fill with eleven elements
10+
for (int i = 0; i < 11; i++) list.add(1);
11+
12+
// Access private elementData via reflection (for demo/learning only)
13+
Field field = ArrayList.class.getDeclaredField("elementData");
14+
field.setAccessible(true);
15+
16+
Object[] elementData = (Object[]) field.get(list);
17+
System.out.println("ArrayList capacity: " + elementData.length);
18+
19+
// Force an addition to trigger resize
20+
list.add(1);
21+
elementData = (Object[]) field.get(list);
22+
System.out.println("ArrayList capacity after adding one more: " + elementData.length);
23+
24+
// Remove many elements to observe size vs capacity
25+
for (int i = 0; i < 8; i++) list.remove(2);
26+
elementData = (Object[]) field.get(list);
27+
System.out.println("ArrayList capacity after removals: " + elementData.length);
28+
29+
// Trim to size to shrink a backing array
30+
list.trimToSize();
31+
elementData = (Object[]) field.get(list);
32+
System.out.println("ArrayList capacity after trimToSize(): " + elementData.length);
33+
}
34+
}

0 commit comments

Comments
 (0)