java中集合的简介说明
转自:
http://www.java265.com/JavaCourse/202204/2947.html
下文笔者将讲述集合的简介说明,如下所示
Collection集合简介
Collection是集合的顶层接口 用于存储一组对象
Collection中常用的方法
| 方法名 | 相关说明 |
| boolean add(E e) | 添加元素 |
| boolean remove(Object o) | 从集合中移除指定元素 |
| void clear() | 清空集合中的元素 |
| boolean contains(Object o) | 判断集合中是否存在指定元素 |
| boolean isEmpty() | 判断集合是否为空 |
| int size() | 返回集合的尺寸 |
例: 创建集合
//创建Collection集合对象
Collection<String> col=new ArrayList<String>();
//Boolean add(E e) 添加元素
col.add("java265.com-1");
col.add("java265.com-2");
//boolean remove(E e) 从集合中移除指定的元素
col.remove("java265.com-2");
//void clear() 清空集合中的元素
col.clear();
//bool contains(E e) 判断集合中是否存在指定的元素
col.contains("java265.com-3");
//boolean isEmpty() 判断集合是否为空
col.isEmpty();
//int size() 集合的长度
col.size();
//遍历集合方式一
Iterator<String> it=col.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
//遍历集合方式二
Object[] objects=col.toArray();
for (int i=0;i<objects.length;i++){
System.out.println(objects[i]);
}
//遍历集合方式三
for (String str :
col) {
System.out.println(str);
}


