Repository navigation
Expand file tree
/
Copy path要练习的代码.txt
More file actions
283 lines (222 loc) · 5.5 KB
/
Copy path要练习的代码.txt
File metadata and controls
283 lines (222 loc) · 5.5 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
集合的操作步骤:
A:创建集合对象
B:创建元素对象
C:把元素添加到集合
D:遍历集合
集合的遍历方式:
Collection:迭代器,增强for
|--List:普通for
|--Set:
字符串(随便给 hello,world,java)
自定义对象(学生类)
class Student {
private String name;
private int age;
public Student() {}
public Student(String name,int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
1:Collection
A:字符串
Collection<String> c = new ArrayList<String>();
c.add("hello");
c.add("world");
c.add("java");
Iterator<String> it = c.iterator();
while(it.hasNext()) {
String s = it.next();
System.out.println(s);
}
for(String s : c) {
System.out.println(s);
}
B:自定义对象
Collection<Student> c = new ArrayList<Student>();
Student s1 = new Student("李连杰",50);
Student s2 = new Student("赵文卓",40);
Student s3 = new Student("吴京",30);
c.add(s1);
c.add(s2);
c.add(s3);
Iterator<Student> it = c.iterator();
while(it.hasNext()) {
Student s = it.next();
System.out.println(s.getName()+"---"+s.getAge());
}
for(Student s : c) {
System.out.println(s.getName()+"---"+s.getAge());
}
2:List
A:字符串
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
Iterator<String> it = list.iterator();
while(it.hasNext()) {
String s = it.next();
System.out.println(s);
}
for(String s : list) {
System.out.println(s);
}
for(int x=0; x<list.size(); x++) {
String s = list.get(x);
System.out.println(s);
}
B:自定义对象
List<Student> list = new ArrayList<Student>();
Student s1 = new Student("李连杰",50);
Student s2 = new Student("赵文卓",40);
Student s3 = new Student("吴京",30);
list.add(s1);
list.add(s2);
list.add(s3);
Iterator<Student> it = list.iterator();
while(it.hasNext()) {
Student s = it.next();
System.out.println(s.getName()+"---"+s.getAge());
}
for(Student s : list) {
System.out.println(s.getName()+"---"+s.getAge());
}
for(int x=0; x<list.size(); x++) {
Student s = list.get(x);
System.out.println(s.getName()+"---"+s.getAge());
}
3:ArrayList(掌握 自己补齐)
A:字符串
ArrayList<String> array = new ArrayList<String>();
array.add("java");
array.add("hello");
array.add("world");
//普通for循环
for(int i =0; i<array.size(); i++) {
String s = array.get(i);
System.out.println(s);
}
//增强for
for(String s : array) {
System.out.println(s);
}
//迭代器
Iterator<String> it = array.iterator();
while(it.hasNext()) {
String s = it.next();
System.out.println(s);
}
B:自定义对象
ArrayList<Student> array = new ArrayList<Student>();
Student s1 = new Student("laozi",1000);
Student s2 = new Student("kongzi",100);
Student s3 = new Student("zhuangzi",10);
array.add(s1);
array.add(s2);
array.add(s3);
//普通for循环
for (int i = 0; i<array.size(); i++) {
Student s = array.get(i);
System.out.println(s.getName()+"---"+s.getAge());
}
//增强for循环
for (Student s : array) {
System.out.println(s.getName()+"---"+s.getAge());
}
//迭代器循环
for(Iterator<Student> it = array.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName()+"---"+s.getAge());
}
4:Vector(自己补齐)
A:字符串
Vector<String> vector = new Voctor<String>();
vector.add("java");
vector.add("hello");
vector.add("world");
//普通for循环
for(int i =0; i<vector.size(); i++) {
String s = vector.get(i);
System.out.println(s);
}
//增强for
for(String s : vector) {
System.out.println(s);
}
//迭代器
Iterator<String> it = vector.iterator();
while(it.hasNext()) {
String s = it.next();
System.out.println(s);
}
B:自定义对象
vectorList<Student> array = new vectorList<Student>();
Student s1 = new Student("laozi",1000);
Student s2 = new Student("kongzi",100);
Student s3 = new Student("zhuangzi",10);
vector.add(s1);
vector.add(s2);
vector.add(s3);
//普通for循环
for (int i = 0; i<vector.size(); i++) {
Student s = vector.get(i);
System.out.println(s.getName()+"---"+s.getAge());
}
//增强for循环
for (Student s : vector) {
System.out.println(s.getName()+"---"+s.getAge());
}
//迭代器循环
for(Iterator<Student> it = vector.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName()+"---"+s.getAge());
}
5:LinkedList(掌握 自己补齐)
6:Set
A:字符串
Set<String> set = new HashSet<String>();
set.add("hello");
set.add("world");
set.add("java");
Iterator<String> it = set.iterator();
while(it.hasNext()) {
String s = it.next();
System.out.println(s);
}
for(String s : set) {
System.out.println(s);
}
B:自定义对象
Set<Student> set = new HashSet<Student>();
Student s1 = new Student("李连杰",50);
Student s2 = new Student("赵文卓",40);
Student s3 = new Student("吴京",30);
set.add(s1);
set.add(s2);
set.add(s3);
Iterator<Student> it = set.iterator();
while(it.hasNext()) {
Student s = it.next();
System.out.println(s.getName()+"---"+s.getAge());
}
for(Student s : set) {
System.out.println(s.getName()+"---"+s.getAge());
}
7:HashSet(掌握 自己补齐)
存储自定义对象的时候,如果成员变量值相同,我们就认为是同一个元素。
8:TreeSet(掌握 自己补齐)
存储自定义对象的时候,如果成员变量值相同,我们就认为是同一个元素。
首先根据年龄从小到大排序,其次在根据姓名的长度排序。