-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayList2.java
More file actions
42 lines (42 loc) · 1.23 KB
/
ArrayList2.java
File metadata and controls
42 lines (42 loc) · 1.23 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
import java.util.*;
public class ArrayList2 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
//add elements
list.add(0);
list.add(2);
list.add(3);
System.out.println(list);
//get elements
int ele = list.get(0);
System.out.println(ele);
//add element at index
list.add(1, 1);
System.out.println(list);
//set element at index
list.set(0,-1);
System.out.println(list);
//delete element at index
list.remove(3);
System.out.println(list);
//size of list
int size = list.size();
System.out.println(size);
//check if list is empty
boolean isEmpty = list.isEmpty();
System.out.println("is the list empty?\t"+isEmpty);
//loop through list
for(int i=0;i<list.size();i++){
System.out.print(list.get(i)+" ");
}
System.out.println();
//loop through list using iterator
Iterator<Integer> it = list.iterator();
while(it.hasNext()){
System.out.print(it.next()+" ");
}
System.out.println();
//sorting
Collections.sort(list);
}
}