Commit 3c03fc3
committed
feat(ArrayListDemo): add demo of ArrayList operations with generics
What
- Added ArrayListDemo class.
- Demonstrated creation of ArrayList with generic type Integer.
- Created:
- al1 with initial capacity 20.
- al2 initialized from List.of(50,60,70,80,90).
- Performed operations:
- add(E) → appended 10.
- add(index, E) → inserted 5 at position 0.
- addAll(Collection) → added all elements from al2 to al1.
- add(index, E) → inserted 70 at index 5.
- get(index) → retrieved element at index 5.
- indexOf(Object) → found first occurrence of 70.
- lastIndexOf(Object) → found last occurrence of 70.
- contains(Object) → checked presence of 50.
- Printed final list contents.
Why
- Demonstrates common ArrayList methods for insertion, search, and membership.
- Shows difference between indexOf() and lastIndexOf() when duplicates exist.
- Illustrates initializing ArrayList with capacity and with another collection.
- Useful as a reference for basic ArrayList API usage.
How
- al1.add(10) → [10].
- al1.add(0,5) → [5, 10].
- al1.addAll(al2) → [5,10,50,60,70,80,90].
- al1.add(5,70) → insert 70 at index 5 → [5,10,50,60,70,70,80,90].
- al1.get(5) → 70.
- al1.indexOf(70) → 4 (first occurrence).
- al1.lastIndexOf(70) → 5 (last occurrence).
- al1.contains(50) → true.
- Final list → [5, 10, 50, 60, 70, 70, 80, 90].
Logic
- Inputs: integers manually added and from al2.
- Outputs: index lookups, boolean membership test, printed list.
- Flow:
1. Populate al1 and al2.
2. Insert elements at specific positions.
3. Add all elements from al2.
4. Perform retrieval and search operations.
5. Print results.
- Edge cases:
- add(index, E) throws IndexOutOfBoundsException if index invalid.
- indexOf() returns -1 if element not found.
- contains() checks equality with equals().
- Complexity:
- get() O(1).
- add(E) amortized O(1).
- add(index, E), addAll(), indexOf(), lastIndexOf(), contains() all O(n).
- Concurrency: ArrayList not thread-safe.
Real-life applications
- Maintaining ordered dynamic lists of items (e.g., user IDs, task queues).
- Searching for items with duplicates and needing first/last occurrence.
- Initializing list with known data, then appending or inserting new values.
Notes
- Initial capacity (20) avoids resizing until >20 elements are added.
- indexOf vs lastIndexOf clarifies handling duplicates.
- retainAll(Collection) (commented out) would keep only elements common to both lists.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent a4128a7 commit 3c03fc3
File tree
1 file changed
+5
-5
lines changed- Section 25 Collections Frameworks/List Interface/ArrayList/src
1 file changed
+5
-5
lines changedLines changed: 5 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | 1 | | |
3 | 2 | | |
4 | | - | |
| 3 | + | |
5 | 4 | | |
6 | | - | |
| 5 | + | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | | - | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
0 commit comments