-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava_ArrayList.java
More file actions
46 lines (32 loc) · 1.17 KB
/
Java_ArrayList.java
File metadata and controls
46 lines (32 loc) · 1.17 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 Java_ArrayList {
public static void main(String[] args) {
ArrayList<String> stringArr = new ArrayList<>();
stringArr.add("Blue");
stringArr.add("Yellow");
stringArr.add("Green");
for (String i : stringArr){
System.out.println(i);
}
stringArr.addFirst("Purple");
String element = stringArr.get(0);
System.out.println("\n" + element);
stringArr.set(3, "Red");
System.out.println("\n" + stringArr.getLast());
stringArr.remove(2);
System.out.println("\n" + stringArr.contains("Purple"));
Collections.sort(stringArr);
System.out.println("\n" + stringArr);
ArrayList optArr = (ArrayList) stringArr.clone();
System.out.println("\n" + optArr);
System.out.println("\n" + stringArr);
Collections.reverse(optArr);
System.out.println("\n" + optArr);
System.out.println(stringArr.equals(optArr));
optArr.clear();
System.out.println(optArr.isEmpty());
stringArr.ensureCapacity(10);
stringArr.trimToSize();
}
}