package test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class collection {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<String>();
collection.add("hello"); // 添加元素
collection.add("world");
collection.remove("world");
collection.clear();
collection.contains("hello"); //bool型判断是否存在
collection.isEmpty(); //判断是否为空
collection.size(); //返回集合中的元素个数
System.out.println(collection);
// 遍历
Iterator<String> iterator = collection.iterator();
System.out.println(iterator.next()); //迭代下一个元素
while (iterator.hasNext()){
String s= iterator.next();
System.out.println(s);
}
}
}
|