Commit 267ed50
committed
feat(ArrayListDemo4): add demo of ArrayList creation and modification methods
What
- Added ArrayListDemo4 class.
- Demonstrated multiple ways to create ArrayLists:
- With an explicit initial capacity (20).
- From an existing collection (List.of(...)).
- Performed various operations:
- add(E) → appended 10.
- add(index, E) → inserted 5 at index 0.
- addAll(index, Collection) → inserted another list starting at index 1.
- set(index, E) → replaced element at index 5 with 70.
- Checked size before calling set(6,100) to avoid IndexOutOfBoundsException, using fallback logic to pad or add if index unavailable.
- Printed final contents.
Why
- Illustrates key ArrayList API methods: add, addAll, set, get.
- Shows safe handling when using set() with indices that may not exist.
- Clarifies difference between setting (replacing) vs adding (increasing size).
How
- al1 created empty with capacity 20.
- al2 initialized with [50,60,70,80,90].
- al1 after operations:
1. add(10) → [10].
2. add(0,5) → [5,10].
3. addAll(1,al2) → [5,50,60,70,80,90,10].
4. set(5,70) → [5,50,60,70,80,70,10].
5. size check for index 6:
- If present, set(6,100).
- Else, pad with zeros and append 100.
- Printed list contents.
Logic
- Inputs: manually added integers and list from List.of().
- Outputs: modified ArrayList contents.
- Flow:
1. Initialize lists.
2. Insert/append elements at positions.
3. Replace elements with set().
4. Print final list.
- Edge cases:
- set(index,E) requires index < size, otherwise IndexOutOfBoundsException.
- addAll(index, collection) throws if index > size.
- Complexity:
- add(E) amortized O(1).
- add(index,E), addAll(index,collection), set(index,E) all O(n) worst case due to shifting.
- Concurrency: ArrayList not thread-safe.
Real-life applications
- Building and modifying ordered lists of data (IDs, records).
- Inserting collections into existing lists at specific positions.
- Safely replacing vs appending items depending on conditions.
- Demonstrates difference between initial capacity and actual size.
Notes
- Initial capacity is a performance optimization; size grows dynamically regardless.
- Use set() only for replacing existing values, not for extending size.
- Example illustrates safe guarding against IndexOutOfBounds by checking size.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 13a4dd8 commit 267ed50
File tree
1 file changed
+36
-0
lines changed- Section 25 Collections Frameworks/List Interface/ArrayList/src
1 file changed
+36
-0
lines changedLines changed: 36 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 | + | |
| 35 | + | |
| 36 | + | |
0 commit comments