package com.bo.collection;
//Collection
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo01 {
public static void main(String[] args) {
//创建一个集合
Collection collection = new ArrayList();
//添加元素
collection.add("苹果");
collection.add("西瓜");
collection.add("榴莲");
System.out.println("元素个数:"+collection.size());
System.out.println(collection);
//删除元素
collection.remove("苹果");
System.out.println("删除后元素个数:"+collection.size());
//清空元素
//collection.clear();
//System.out.println("清空元素后个数:"+collection.size());
//遍历元素
System.out.println("---------------增强for----------");
for (Object object:collection) {
System.out.println(object);
}
System.out.println("------------迭代器(专门用来遍历集合)--------------");
Iterator it=collection.iterator();
while(it.hasNext()){
String s =(String) it.next();
System.out.println(s);
//不能使用collection.remove()删除方法
//it.remove();//删除用it.remove
}
System.out.println("删除之后:"+collection.size());
//判断
System.out.println(collection.contains("西瓜"));//有没有西瓜
System.out.println(collection.isEmpty());//是不是空的
}
}