-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayLists.java
43 lines (35 loc) · 1.11 KB
/
ArrayLists.java
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
import java.util.*;
public class ArrayLists {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
// ArrayList<String> list2 = new ArrayList<>();
//add elements
list.add(1);
list.add(2);
list.add(3);
System.out.println(list);
//to get an element
int el = list.get(0);
System.out.println(el); // 1
//add element in between
list.add(1,5);
System.out.println(list); // 1 5 2 3
//set element
list.set(0,0);
System.out.println(list); // 0 1 5 2 3
//remove/delete element
list.remove(3);
System.out.println(list);
//calculate length or size of the list
int size = list.size();
System.out.println(size);
//Loops on lists
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i)+" ");
}
System.out.println();
//Sorting the list
Collections.sort(list);
System.out.println(list);
}
}