Skip to content

Commit 5ea07ab

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

1 file changed

+8
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package ListDemo.ArrayListLinkedListStack;
2-
/*
3-
Write a program that does the following.
1+
/* Write a program that does the following.
42
5-
Create an ArrayList of integers, add the elements [12, 25, 34, 46] to it Remove the number 25 from the ArrayList
3+
Create an ArrayList of integers, add the elements [12, 25, 34, 46]
4+
to it Remove the number 25 from the ArrayList.
65
76
Print the final ArrayList.
87
*/
8+
99
import java.util.*;
1010

11-
class CodeChef {
11+
class CodeChefDemo {
1212
public static void main(String[] args) {
1313
ArrayList<Integer> list = new ArrayList<>();
1414
list.add(12);
@@ -17,7 +17,7 @@ public static void main(String[] args) {
1717
list.add(46);
1818

1919
Integer RemoveANumber = list.remove(1);
20-
System.out.println("Number to be removed: "+RemoveANumber);
21-
System.out.println("Final List: "+list);
20+
System.out.println("Number to be removed: "+ RemoveANumber);
21+
System.out.println("Final List: " + list);
2222
}
23-
}
23+
}

0 commit comments

Comments
 (0)