Contents
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

package lianxi;

import java.util.*;
import java.io.*;

public class lianxix {
public static void main(String[] args)
{

ArrayList list1 = new ArrayList();
list1.add("one");
list1.add("two");
list1.add("three");
list1.add("four");
list1.add("five");

list1.add(0,"zero");

System.out.println("<--list1中共有> " + list1.size()+ "个元素");
System.out.println("<--list1中的内容:" + list1 + "-->");

ArrayList list2 = new ArrayList(50);
list2.add("Begin");
list2.addAll(list1);
list2.add("End");

System.out.println("<--list2中共有>" + list2.size()+ "个元素");
System.out.println("<--list2中的内容:" + list2 + "-->");

ArrayList list3 = new ArrayList(list1);
System.out.println("list3: " + list3);
list3.removeAll(list1);
System.out.println("<--list3中是否存在one: "+ (list3.contains("one")? "是":"否")+ "-->");

list3.add(0,"same element");
list3.add(1,"same element");
System.out.println("<--list3中共有>" + list3.size()+ "个元素");
System.out.println("<--list3中的内容:" + list3 + "-->");
System.out.println("<--list3中第一次出现same element的索引是" + list3.indexOf("same element") + "-->");
System.out.println("<--list3中最后一次出现same element的索引是" + list3.lastIndexOf("same element") + "-->");

System.out.println("<--使用Iterator接口访问list3->");
Iterator it = list3.iterator();
System.out.println("<--list3中的元素: -->");
while(it.hasNext()){
String str = (String)it.next(); //ArrayList类数组存的是object,所以取出时要转换,除非一开始就ArrayList<String>
System.out.println(str);
}

System.out.println("<--将list3中的same element修改为another element-->");
list3.set(0,"another element");
list3.set(1,"another element");
System.out.println(list3);

System.out.println("<--将list3转为数组-->");
// Object [] array =(Object[]) list3.toArray(new Object[list3.size()] );
Object [] array = list3.toArray();
for(int i = 0; i < array.length ; i ++){
String str = (String)array[i];
System.out.println("array[" + i + "] = "+ str);
}

System.out.println("<---清空list3->");
list3.clear();
System.out.println("<--list3中是否为空: " + (list3.isEmpty()?"是":"否") + "-->");
System.out.println("<--list3中共有>" + list3.size()+ "个元素");
}
}


<--list1中共有> 6个元素
<--list1中的内容:[zero, one, two, three, four, five]-->
<--list2中共有>8个元素
<--list2中的内容:[Begin, zero, one, two, three, four, five, End]-->
list3: [zero, one, two, three, four, five]
<--list3中是否存在one: 否-->
<--list3中共有>2个元素
<--list3中的内容:[same element, same element]-->
<--list3中第一次出现same element的索引是0-->
<--list3中最后一次出现same element的索引是1-->
<--使用Iterator接口访问list3->
<--list3中的元素: -->
same element
same element
<--将list3中的same element修改为another element-->
[another element, another element]
<--将list3转为数组-->
array[0] = another element
array[1] = another element
<---清空list3->
<--list3中是否为空: 是-->
<--list3中共有>0个元素
Contents