Skip to content

Commit be34c11

Browse files
committed
feat: Demonstrate basic usage of ArrayList with user input in Java
🧠 Logic: - Creates an `ArrayList<Integer>` with initial capacity of 5. - Takes 5 user inputs and stores them in the list using `add()`. - Prints each element using `get(index)` since array-style indexing doesn't apply to ArrayList. - Shows example usages (commented): `contains()`, `set()`, `remove()`, and printing the list. Signed-off-by: Somesh diwan <[email protected]>
1 parent 8b6efd6 commit be34c11

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

Section9ArrayAB/Array IMP/ArrayListExample.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
public class ArrayListExample {
55
public static void main(String[] args) {
66
Scanner in = new Scanner(System.in);
7+
8+
System.out.println("Enter 5 numbers: ");
79
// Syntax
810
ArrayList<Integer> list = new ArrayList<>(5);
911

@@ -31,10 +33,6 @@ public static void main(String[] args) {
3133
for (int i = 0; i < 5; i++) {
3234
System.out.println(list.get(i)); // pass index here, list[index] syntax will not work here
3335
}
34-
3536
System.out.println(list);
36-
37-
38-
3937
}
4038
}

0 commit comments

Comments
 (0)