-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathArrayListExample.java
More file actions
32 lines (26 loc) · 984 Bytes
/
ArrayListExample.java
File metadata and controls
32 lines (26 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package ArrayVSArraylist;
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
// Declare and initialize an ArrayList of integers
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
// Access elements in the ArrayList
System.out.println("ArrayList elements:");
for (int i = 0; i < numbers.size(); i++) {
System.out.println(numbers.get(i));
}
// Add a new element
numbers.add(6);
System.out.println("After adding an element: " + numbers);
// Size of the ArrayList can grow dynamically
System.out.println("ArrayList size: " + numbers.size());
// Removing an element
numbers.remove(0); // Removes the element at index 0
System.out.println("After removing an element: " + numbers);
}
}