Commit 5e0b97d
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- Section 25 Collections Frameworks/List Interface/ArrayList/src
1 file changed
+14
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | 1 | | |
4 | 2 | | |
5 | | - | |
| 3 | + | |
6 | 4 | | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
10 | 8 | | |
11 | 9 | | |
12 | 10 | | |
13 | | - | |
14 | | - | |
15 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
| 20 | + | |
27 | 21 | | |
28 | 22 | | |
29 | 23 | | |
30 | | - | |
| 24 | + | |
31 | 25 | | |
32 | 26 | | |
33 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
34 | 32 | | |
35 | | - | |
| 33 | + | |
0 commit comments