Skip to content

Commit 13a4dd8

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

1 file changed

+3
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
package ListDemo.ArrayListLinkedListStack;
2-
31
import java.util.ArrayList;
42
import java.util.List;
53

6-
public class ListED2 {
4+
public class ArrayListDemo3 {
75
public static void main(String[] args) {
86
List<Integer> list = new ArrayList<>();
97
list.add(1);
@@ -14,12 +12,11 @@ public static void main(String[] args) {
1412

1513
//Very Important.
1614
list.remove(Integer.valueOf(1));
17-
1815
System.out.println(list);
1916

2017
List<String> fruits = new ArrayList<>();
21-
fruits.add("Apple");
2218

19+
fruits.add("Apple");
2320
fruits.add("Apple");
2421
fruits.add("Apple");
2522
fruits.add("Banana");
@@ -30,4 +27,4 @@ public static void main(String[] args) {
3027
fruits.remove(fruits.remove(1));
3128
System.out.println(fruits);
3229
}
33-
}
30+
}

0 commit comments

Comments
 (0)