欢迎访问CSDN博客专栏CSDN专栏 Java全栈之路,Github主页

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

import java.util.LinkedList;
import java.util.List;

public class TestList {

public static void main(String[] args) {

// 声明List接口的引用指向实现类的对象,形成多态
List l1 = new LinkedList();

// 向集合中增加元素,没有指定下标位置时,默认都是插入到末尾
boolean b1 = l1.add(new Integer(1));
System.out.println("b1 = " + b1); // true
System.out.println("l1 = " + l1); // [1]
b1 = l1.add(new Integer(2));
System.out.println("b1 = " + b1); // true
System.out.println("l1 = " + l1); // [1, 2]

// 向下标为0的位置插入元素"one"
l1.add(0, new String("one"));
System.out.println("l1 = " + l1); // [one, 1, 2]
// 向下标为2的位置插入元素"two"
l1.add(2, new String("two"));
System.out.println("l1 = " + l1); // [one, 1, two, 2]
// 向下标为4的位置插入元素3
l1.add(4, new Integer(3));
System.out.println("l1 = " + l1); // [one, 1, two, 2, 3]

System.out.println("---------------------------------------------");

//准备另外一个集合
List l2 = new LinkedList();

l2.add("three");
l2.add(4);
System.out.println("l2 = " + l2); // [three, 4]
//将集合l2中的所有元素插入到集合l1中
l1.addAll(l2);
System.out.println("l1 = " + l1); // [one, 1, two, 2, 3, three, 4]
System.out.println("l2 = " + l2); // [three, 4]
//将集合l2中的所有元素插入到集合l1中下标为0的位置
l1.addAll(0, l2);
System.out.println("l1 = " + l1); // [three, 4, one, 1, two, 2, 3, three, 4]
System.out.println("l2 = " + l2); // [three, 4]

System.out.println("---------------------------------------------");

//实现集合中元素的修改
//将集合l1中的three 和 one 分别修改为 3 和 1
//由add()方法的提示可知:形参类型为Object类型,因此放入的每个元素本质上都是
// 看做Object类型处理,因此从集合中取出该元素时就是Object类型。
//引用类型之间的转换必须有父子类关系,当转换的目标类型不是该引用真正指向的
// 类型时,编译不报错,运行阶段产生类型转换异常。
String str = (String) l1.set(0, 3);
System.out.println("str = " + str); //str = three
// [3, 4, one, 1, two, 2, 3, three, 4]
System.out.println("l1 = " + l1);

str = (String) l1.set(2, 1);
System.out.println("str = " + str); //str = one
// [3, 4, 1, 1, two, 2, 3, three, 4]
System.out.println("l1 = " + l1);

//将集合l1中的4改为four
Integer it1 = (Integer) l1.set(1, "four");
System.out.println("it1 = " + it1); //4
// [3, four, 1, 1, two, 2, 3, three, 4]
System.out.println("l1 = " + l1);

System.out.println("---------------------------------------------");

//实现集合中元素的获取
//获取集合l1中下标为0的元素
it1 = (Integer) l1.get(0);
System.out.println("it1 = " + it1); //3
// [3, four, 1, 1, two, 2, 3, three, 4]
System.out.println("l1 = " + l1);

//获取集合l1中下标为1的元素
str = (String) l1.get(1);
System.out.println("str = " + str); //four
// [3, four, 1, 1, two, 2, 3, three, 4] String类型的整体
System.out.println("l1 = " + l1);

System.out.println("---------------------------------------");
//练习:编程使用get()方法将集合l1中的所有元素一个一个取出来并打印
System.out.print("l1 = [");
for(int i = 0; i < l1.size(); i++){
//当打印最后一个元素时,不打印, 而是打印]
if(i == l1.size()-1){
//get()返回Object类型的,返回每一个独立的元素
System.out.println(l1.get(i) + "]");
}
else{
//打印除了最后一个元素之外的元素
System.out.print(l1.get(i) + ", ");
}
}
System.out.println();

System.out.println("---------------------------------------");
//编程实现集合l1中所有元素的删除
//l1.clear();
/*for(int i = l1.size()-1; i >= 0; i--){
System.out.println("删除的元素是:" + l1.remove(i));
}*/
System.out.println("l1 = " + l1); //l1 = [啥也没有]

System.out.println("---------------------------------------");

//获取集合l1中的部分内容
List l3 = l1.subList(3, 8);
System.out.println("l3 = " + l3); //[1, two, 2, 3, three]
//[3, four, 1, 1, two, 2, 3, three, 4]
System.out.println("l1 = " + l1);

System.out.println("集合l3中删除的元素是:" + l3.remove(0));
System.out.println("l3 = " + l3); //[two, 2, 3, three]
//[3, four, 1, two, 2, 3, three, 4]
System.out.println("l1 = " + l1);

}

}