-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaggregation1.java
More file actions
70 lines (57 loc) · 2.1 KB
/
aggregation1.java
File metadata and controls
70 lines (57 loc) · 2.1 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
/*
* @由于个人水平有限, 难免有些错误, 还请指点:
* @Author: cpu_code
* @Date: 2020-09-16 16:08:20
* @LastEditTime: 2020-09-16 16:09:29
* @FilePath: \java\aggregation\aggregation1.java
* @Gitee: [https://gitee.com/cpu_code](https://gitee.com/cpu_code)
* @Github: [https://github.com/CPU-Code](https://github.com/CPU-Code)
* @CSDN: [https://blog.csdn.net/qq_44226094](https://blog.csdn.net/qq_44226094)
* @Gitbook: [https://923992029.gitbook.io/cpucode/](https://923992029.gitbook.io/cpucode/)
*/
package aggregation;
import java.util.ArrayList;
import java.util.Collection;
public class aggregation1
{
public static void main(String[] args) {
// 创建集合对象
// 使用多态形式
Collection<String> coll = new ArrayList<String>();
// 使用方法
// 添加功能 boolean add(String s)
coll.add("cpucode");
coll.add("cpu");
System.out.println(coll);
// boolean contains(E e) 判断o是否在集合中存在
System.out.println("判断 cpucode 是不是在集合里" + coll.contains("cpucode"));
System.out.println("判断 cpu 是不是在集合里" + coll.contains("cpu"));
//boolean remove(E e) 删除在集合中的o元素
System.out.println("删除cpu: " + coll.remove("cpu"));
System.out.println("操作后的集合:" + coll);
// size() 集合中有几个元素
System.out.println("集合有 " + coll.size() + "个元素");
// Object[] toArray()转换成一个Object数组
Object[] objects = coll.toArray();
// 遍历数组
for(int i = 0; i < objects.length; i++){
System.out.println(objects[i]);
}
// void clear() 清空集合
coll.clear();
System.out.println("集合内容 :" + coll);
// boolean isEmpty() 判断是否为空
System.out.println(coll.isEmpty());
}
}
/*
[cpucode, cpu]
判断 cpucode 是不是在集合里true
判断 cpu 是不是在集合里true
删除cpu: true
操作后的集合:[cpucode]
集合有 1个元素
cpucode
集合内容 :[]
true
*/