练习程序
package _4;
import java.util.ArrayList;
import java.util.Collection;
//Collection集合常用方法
//boolean add(E e)添加
//boolean remove(Object o)移除
//void clear()清空
//boolean contains(Object 0)判断元素是否存在
//boolean isEmpty()判断是否为空
//int size()元素个数
public class _222 {
public static void main(String[] args) {
Collection c = new ArrayList();
//boolean add(E e)添加
System.out.println(c.add("hello"));
System.out.println(c.add("world"));
System.out.println(c.add("world"));
//boolean remove(Object o)移除
System.out.println(c.remove("world"));
System.out.println(c.remove("qweer"));
//void clear()清空
// c.clear();
//boolean contains(Object 0)判断元素是否存在
System.out.println(c.contains("world"));
System.out.println(c.contains("sdrev"));
//boolean isEmpty()判断是否为空
System.out.println(c.isEmpty());
//int size()元素个数
System.out.println(c.size());
//输出集合
System.out.println(c);
}
}
遍历
练习程序
package _4;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
//Collection遍历
public class _223 {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");
Iterator it = c.iterator();
//E next()返回迭代中的下一个元素
//boolean hasNext()如果迭代具有更多元素,则返回true
//用while改进
while (it.hasNext()){
//System.out.println(it.next());
String s=it.next();
System.out.println(s);
}
}
}



