Skip to content

Commit 3c03fc3

Browse files
committed
feat(ArrayListDemo): add demo of ArrayList operations with generics
What - Added ArrayListDemo class. - Demonstrated creation of ArrayList with generic type Integer. - Created: - al1 with initial capacity 20. - al2 initialized from List.of(50,60,70,80,90). - Performed operations: - add(E) → appended 10. - add(index, E) → inserted 5 at position 0. - addAll(Collection) → added all elements from al2 to al1. - add(index, E) → inserted 70 at index 5. - get(index) → retrieved element at index 5. - indexOf(Object) → found first occurrence of 70. - lastIndexOf(Object) → found last occurrence of 70. - contains(Object) → checked presence of 50. - Printed final list contents. Why - Demonstrates common ArrayList methods for insertion, search, and membership. - Shows difference between indexOf() and lastIndexOf() when duplicates exist. - Illustrates initializing ArrayList with capacity and with another collection. - Useful as a reference for basic ArrayList API usage. How - al1.add(10) → [10]. - al1.add(0,5) → [5, 10]. - al1.addAll(al2) → [5,10,50,60,70,80,90]. - al1.add(5,70) → insert 70 at index 5 → [5,10,50,60,70,70,80,90]. - al1.get(5) → 70. - al1.indexOf(70) → 4 (first occurrence). - al1.lastIndexOf(70) → 5 (last occurrence). - al1.contains(50) → true. - Final list → [5, 10, 50, 60, 70, 70, 80, 90]. Logic - Inputs: integers manually added and from al2. - Outputs: index lookups, boolean membership test, printed list. - Flow: 1. Populate al1 and al2. 2. Insert elements at specific positions. 3. Add all elements from al2. 4. Perform retrieval and search operations. 5. Print results. - Edge cases: - add(index, E) throws IndexOutOfBoundsException if index invalid. - indexOf() returns -1 if element not found. - contains() checks equality with equals(). - Complexity: - get() O(1). - add(E) amortized O(1). - add(index, E), addAll(), indexOf(), lastIndexOf(), contains() all O(n). - Concurrency: ArrayList not thread-safe. Real-life applications - Maintaining ordered dynamic lists of items (e.g., user IDs, task queues). - Searching for items with duplicates and needing first/last occurrence. - Initializing list with known data, then appending or inserting new values. Notes - Initial capacity (20) avoids resizing until >20 elements are added. - indexOf vs lastIndexOf clarifies handling duplicates. - retainAll(Collection) (commented out) would keep only elements common to both lists. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent a4128a7 commit 3c03fc3

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

Section25CollectionFramework/src/ListDemo/ListDemo.java renamed to Section 25 Collections Frameworks/List Interface/ArrayList/src/ArrayListDemo.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
package ListDemo;
21
import java.util.*;
32

4-
public class ListDemo {
3+
public class ArrayListDemo {
54
public static void main(String[] args) {
6-
//Object of array list and it ie generic type. Allowed integer type data
5+
//Object of an array list and it is a generic type. Allowed integer type data.
6+
77
ArrayList<Integer> al1 = new ArrayList<>(20);
88

99
ArrayList<Integer> al2 = new ArrayList<>(List.of(50,60,70,80,90));
1010

1111
al1.add(10);
1212
al1.add(0,5);
13+
1314
//al1.addALl(1,al2);
1415

1516
al1.addAll(al2);
1617
al1.add(5,70);
1718

1819
System.out.println(al1.get(5));
19-
2020
System.out.println(al1.indexOf(70));
2121
System.out.println(al1.lastIndexOf(70));
2222

23-
//Checking whether the element present inside the list or not.
23+
//Checking whether the element presents inside the list or not.
2424
System.out.println(al1.contains(50));
2525

2626
//al1.retainAll(al2);

0 commit comments

Comments
 (0)