forked from eaglesky/JavaPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestArrayList.java
More file actions
46 lines (39 loc) · 1.14 KB
/
TestArrayList.java
File metadata and controls
46 lines (39 loc) · 1.14 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.List;
import java.util.ArrayList;
import java.util.Collections;
public class TestArrayList {
public static void main (String[] args) {
int a1 = 3, a2 = 5, a3 = 7;
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(a1);
arr.add(a2);
arr.add(a3);
System.out.println("Array size = " + arr.size());
for (int a : arr)
{
System.out.print(a + " ");
}
System.out.println("");
for (int i = 0; i < arr.size(); ++i) {
System.out.print(arr.get(i) + " ");
}
System.out.println("");
Collections.reverse(arr);
System.out.println("After reversing the original arraylist: ");
for (int a : arr)
{
System.out.print(a + " ");
}
System.out.println("");
System.out.println("arr contains a1 ? " + arr.contains(3));
System.out.println("Index of a1 : " + arr.indexOf(a1));
List<Integer> arrClone = new ArrayList<>(arr); //shallow copy
System.out.println("arrClone:");
System.out.println(arrClone);
List<Object> objectArray = new ArrayList<>();
objectArray.add(new Cat(1, 2, 3));
objectArray.add(new Animal());
objectArray.add(new Person("Allen", 23));
System.out.println(objectArray);
}
}