forked from Joyounger/Introduction-to-Java-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestArrayList.java
More file actions
56 lines (44 loc) · 1.63 KB
/
TestArrayList.java
File metadata and controls
56 lines (44 loc) · 1.63 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
47
48
49
50
51
52
53
54
55
56
// date:17.6.28
// author: aquanox@163.com
/* compile and run
kolya@asus ~/src/Introduction-to-Java-Programming $ javac chap9/TestArrayList.java
注: chap9/TestArrayList.java使用了未经检查或不安全的操作。
注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
kolya@asus ~/src/Introduction-to-Java-Programming $ java chap9/TestArrayList
*/
package chap9;
public class TestArrayList {
public static void main(String[] args) {
java.util.ArrayList cityList = new java.util.ArrayList();
cityList.add("London");
cityList.add("New York");
cityList.add("Paris");
cityList.add("Toronto");
cityList.add("Hong Kong");
cityList.add("Singapore");
System.out.println("List size? " + cityList.size());
System.out.println("is Toronto in the list? " + cityList.contains("Toronto"));
System.out.println("the location of new york in the list is " + cityList.indexOf("New York"));
System.out.println("is the list empty? " + cityList.isEmpty());
cityList.add(2, "Beijing");
cityList.remove("Toronto");
cityList.remove(1);
for (int i = 0; i < cityList.size(); i++) {
System.out.print(cityList.get(i) + " ");
}
System.out.println();
java.util.ArrayList list = new java.util.ArrayList();
list.add(new Circle(2));
list.add(new Circle(3));
System.out.println("the area of the circle? " + ((Circle)list.get(0)).getArea());
}
}
/*
kolya@asus ~/src/Introduction-to-Java-Programming $ java chap9/TestArrayList
List size? 6
is Toronto in the list? true
the location of new york in the list is 1
is the list empty? false
London Beijing Paris Hong Kong Singapore
the area of the circle? 12.566370614359172
*/