Skip to content

Commit 267ed50

Browse files
committed
feat(ArrayListDemo4): add demo of ArrayList creation and modification methods
What - Added ArrayListDemo4 class. - Demonstrated multiple ways to create ArrayLists: - With an explicit initial capacity (20). - From an existing collection (List.of(...)). - Performed various operations: - add(E) → appended 10. - add(index, E) → inserted 5 at index 0. - addAll(index, Collection) → inserted another list starting at index 1. - set(index, E) → replaced element at index 5 with 70. - Checked size before calling set(6,100) to avoid IndexOutOfBoundsException, using fallback logic to pad or add if index unavailable. - Printed final contents. Why - Illustrates key ArrayList API methods: add, addAll, set, get. - Shows safe handling when using set() with indices that may not exist. - Clarifies difference between setting (replacing) vs adding (increasing size). How - al1 created empty with capacity 20. - al2 initialized with [50,60,70,80,90]. - al1 after operations: 1. add(10) → [10]. 2. add(0,5) → [5,10]. 3. addAll(1,al2) → [5,50,60,70,80,90,10]. 4. set(5,70) → [5,50,60,70,80,70,10]. 5. size check for index 6: - If present, set(6,100). - Else, pad with zeros and append 100. - Printed list contents. Logic - Inputs: manually added integers and list from List.of(). - Outputs: modified ArrayList contents. - Flow: 1. Initialize lists. 2. Insert/append elements at positions. 3. Replace elements with set(). 4. Print final list. - Edge cases: - set(index,E) requires index < size, otherwise IndexOutOfBoundsException. - addAll(index, collection) throws if index > size. - Complexity: - add(E) amortized O(1). - add(index,E), addAll(index,collection), set(index,E) all O(n) worst case due to shifting. - Concurrency: ArrayList not thread-safe. Real-life applications - Building and modifying ordered lists of data (IDs, records). - Inserting collections into existing lists at specific positions. - Safely replacing vs appending items depending on conditions. - Demonstrates difference between initial capacity and actual size. Notes - Initial capacity is a performance optimization; size grows dynamically regardless. - Use set() only for replacing existing values, not for extending size. - Example illustrates safe guarding against IndexOutOfBounds by checking size. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 13a4dd8 commit 267ed50

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
/* Demonstrates basic ArrayList usage:
5+
* - creation with initial capacity
6+
* - creation from another collection
7+
* - add, add at index, addAll at index
8+
* - set(index, value) and get
9+
*/
10+
11+
public class ArrayListDemo4 {
12+
public static void main(String[] args) {
13+
// Object of ArrayList with initial capacity 20
14+
ArrayList<Integer> al1 = new ArrayList<>(20);
15+
16+
// Create from an existing collection
17+
ArrayList<Integer> al2 = new ArrayList<>(List.of(50, 60, 70, 80, 90));
18+
19+
al1.add(10);
20+
al1.add(0, 5); // insert 5 at index 0
21+
al1.addAll(1, al2); // insert all elements of al2 starting at index 1
22+
al1.set(5, 70); // replace element at index 5
23+
24+
// Note: set(6,100) would throw IndexOutOfBounds if index 6 doesn't exist.
25+
// To match the original, guard against OOB:
26+
if (al1.size() > 6) {
27+
al1.set(6, 100);
28+
} else {
29+
// safely add if the index doesn't exist
30+
while (al1.size() < 6) al1.add(0);
31+
al1.add(100);
32+
}
33+
34+
System.out.println("ArrayList contents: " + al1);
35+
}
36+
}

0 commit comments

Comments
 (0)