Skip to content

Commit 5e0b97d

Browse files
committed
feat(ArrayListDemo2): demonstrate basic ArrayList operations (add, contains, iteration)
What - Added ArrayListDemo2 class. - Created an ArrayList<Integer> with default capacity (10). - Inserted three elements: 1, 5, 80. - Demonstrated: - contains(Object) → check for presence of values. - Iteration using enhanced for-each loop. - Iteration using index-based for loop. - Included commented examples showing: - get(index) usage. - size() retrieval. - remove(index), add(index, value), and set(index, value) for element modification. Why - Provides a hands-on demo of common ArrayList methods. - Highlights difference between size and capacity. - Shows safe element access vs exceptions when index is out of bounds. - Illustrates multiple iteration styles. How - List.add(1), add(5), add(80) → List = [1, 5, 80]. - List.contains(5) → true. - List.contains(50) → false. - For-each loop prints elements one by one. - Index-based for loop uses size() and get(i) to print elements. Logic - Inputs: integers 1, 5, 80. - Outputs: - Boolean results for contains() checks. - Printed list elements via two loop styles. - Flow: 1. Create ArrayList. 2. Add three elements. 3. Test membership (contains). 4. Iterate with for-each. 5. Iterate with indexed loop. - Edge cases: - get(3) would throw IndexOutOfBoundsException since size=3 (valid indices 0–2). - remove(index) throws if index invalid. - Complexity: - add(E): amortized O(1). - contains(Object): O(n). - get(index): O(1). - remove(index), add(index,E), set(index,E): O(n) worst case due to shifting. - Concurrency: ArrayList not thread-safe. Real-life applications - Building simple lists of integers, objects, or IDs. - Membership checks with contains(). - Choosing iteration style depending on readability vs need for indices. - Demonstrating dynamic growth compared to primitive arrays. Notes - Default capacity = 10, grows as needed. - For-each is concise for read-only traversal. - Index-based loop useful when modifying elements or needing positions. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 3c03fc3 commit 5e0b97d

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
1-
package ListDemo.ArrayListLinkedListStack;
2-
31
import java.util.ArrayList;
42

5-
public class ListED {
3+
public class ArrayListDemo2 {
64
public static void main(String[] args) {
75
ArrayList<Integer> List = new ArrayList<>(); //Default capacity 10.
86

97
List.add(1); //Index Zero
108
List.add(5); //Index One
119
List.add(80); //Index Two
1210

13-
//System.out.println(List.get(3)); //Throw exception
14-
//System.out.println(List.get(2));
15-
//System.out.println(List.size());
11+
// System.out.println(List.get(3));
12+
// Throw exception
13+
14+
// System.out.println(List.get(2));
15+
// System.out.println(List.size());
1616

1717
System.out.println(List.contains(5));
1818
System.out.println(List.contains(50));
1919

20-
/* for(int i=0; i<List.size(); i++)
21-
{
22-
System.out.println(List.get(i));
23-
}
24-
*/
25-
26-
/*
20+
/*
2721
List.remove(2);
2822
List.add(2,100);
2923
List.set(2,1000);
30-
*/
24+
*/
3125

3226
for(int x:List)
33-
System.out.println(x);
27+
System.out.print(x+" "+"\n");
28+
29+
for(int i=0; i<List.size(); i++) {
30+
System.out.print(List.get(i)+" ");
31+
}
3432
}
35-
}
33+
}

0 commit comments

Comments
 (0)