Commit 13a4dd8
committed
feat(ArrayListDemo3): clarify remove() overload behavior in ArrayList
What
- Added ArrayListDemo3 class.
- Created List<Integer> list = [1,2,3].
- Demonstrated two remove() overloads:
- remove(int index) → removes by position.
- remove(Object o) → removes first occurrence of matching object.
- Used list.remove(Integer.valueOf(1)) to remove the element `1` by value (not index).
- Created List<String> fruits with duplicates.
- Demonstrated nested call fruits.remove(fruits.remove(1)):
- Inner remove(1) removes element at index 1, returns the removed value.
- Outer remove(Object) uses that returned value as argument, removing first matching occurrence.
Why
- Important to distinguish remove(int) vs remove(Object) when working with lists of boxed types like Integer.
- Demonstrates tricky cases where overload resolution can confuse developers.
- Shows side effects of nested remove() calls on lists with duplicates.
How
- For integers:
- list = [1,2,3].
- list.remove(Integer.valueOf(1)) → removes value `1`, leaving [2,3].
- For fruits:
- fruits = ["Apple","Apple","Apple","Banana","Cherry","Banana","Date"].
- fruits.remove(1) → removes index 1 element ("Apple"), returns "Apple".
- fruits.remove("Apple") → removes first occurrence of "Apple" in remaining list.
- Final list = ["Apple","Banana","Cherry","Banana","Date"].
Logic
- Inputs: initial integer and string lists.
- Outputs: modified lists after removal operations.
- Flow:
1. Add elements to lists.
2. Remove integer by value, not index.
3. Remove string twice (nested remove call).
- Edge cases:
- Passing `1` directly to remove() on List<Integer> would remove index 1 (second element), not the value `1`.
- remove(Object) only removes first match; duplicates remain.
- Complexity: O(n) per remove due to shifting.
- Concurrency: ArrayList not thread-safe.
Real-life applications
- Handling data structures where IDs (integers) need to be removed by value, not position.
- Removing duplicate entries step by step.
- Understanding overloaded methods prevents subtle bugs (especially with autoboxing).
Notes
- To avoid ambiguity, explicitly use Integer.valueOf(x) for remove(Object) when working with Integer lists.
- Nested removes are legal but can reduce readability; better to split into two statements for clarity.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 5e0b97d commit 13a4dd8
File tree
1 file changed
+3
-6
lines changed- Section 25 Collections Frameworks/List Interface/ArrayList/src
1 file changed
+3
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | 1 | | |
4 | 2 | | |
5 | 3 | | |
6 | | - | |
| 4 | + | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
| |||
14 | 12 | | |
15 | 13 | | |
16 | 14 | | |
17 | | - | |
18 | 15 | | |
19 | 16 | | |
20 | 17 | | |
21 | | - | |
22 | 18 | | |
| 19 | + | |
23 | 20 | | |
24 | 21 | | |
25 | 22 | | |
| |||
30 | 27 | | |
31 | 28 | | |
32 | 29 | | |
33 | | - | |
| 30 | + | |
0 commit comments