-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListExample.java
More file actions
46 lines (34 loc) · 1.15 KB
/
Copy pathArrayListExample.java
File metadata and controls
46 lines (34 loc) · 1.15 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
// Adding elements to the list
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
// Printing the initial list
System.out.println("Initial list: " + list);
// Sorting the list in ascending order
Collections.sort(list);
// Printing the sorted list
System.out.println("Sorted list (ascending): " + list);
// Sorting the list in descending order
Collections.sort(list, Collections.reverseOrder());
// Printing the sorted list
System.out.println("Sorted list (descending): " + list);
// Removing the first element
list.remove(0);
// Printing the updated list
System.out.println("Updated list after removing the first element: " + list);
// Printing the size of the list
System.out.println("Size of list: " + list.size());
// Looping through the list
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
System.out.println();
}
}