Commit 5ea07ab
committed
feat(CodeChefDemo): add simple ArrayList demo with remove operation
What
- Added CodeChefDemo class.
- Created an ArrayList<Integer>.
- Added elements [12, 25, 34, 46].
- Removed element at index 1 (value = 25).
- Stored removed value in variable RemoveANumber.
- Printed the removed number and the final list.
Why
- Demonstrates ArrayList usage for basic insertion and deletion.
- Shows difference between remove(int index) and remove(Object o).
- Provides clear output verifying which element was removed and what remains in the list.
How
- list.add(...) → list = [12,25,34,46].
- list.remove(1) → removes index 1 element (25), returns it.
- After removal → list = [12,34,46].
- Print removed number and final list.
Logic
- Inputs: integers added to list.
- Outputs:
- Removed number = 25.
- Final list = [12,34,46].
- Flow:
1. Initialize empty ArrayList.
2. Add four integers.
3. Remove element at index 1.
4. Print removed element and final list.
- Edge cases:
- If index out of range, throws IndexOutOfBoundsException.
- If remove(Object) were used instead, it would remove the first matching occurrence.
- Complexity:
- add(E): amortized O(1).
- remove(index): O(n) due to element shifting.
- Concurrency: ArrayList not thread-safe.
Real-life applications
- Managing dynamic lists where elements are frequently added/removed.
- Handling user-selected item deletions in apps.
- Useful exercise to understand remove() overload behavior.
Notes
- remove(int index) vs remove(Object o):
- remove(1) → removes element at index 1.
- remove(Integer.valueOf(25)) → removes element with value 25.
- In this example, both approaches work since we know 25 is at index 1.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent fc2b9b6 commit 5ea07ab
File tree
1 file changed
+8
-8
lines changed- Section 25 Collections Frameworks/List Interface/ArrayList/src
1 file changed
+8
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
| 1 | + | |
4 | 2 | | |
5 | | - | |
| 3 | + | |
| 4 | + | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | | - | |
| 20 | + | |
| 21 | + | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
0 commit comments