java 中的Collection

 1 /*
 2  *一、 Collection?-------->容器!
 3  * 
 4  * 1.来源于java.util包 非常实用的数据结构! 
 5  * 
 6  *二、 方法?
 7  * 
 8  *  void clear()删除集合中所有元素
 9  *     boolean add(Object o)添加对象到集合
10     boolean remove(Object o)删除指定的对象
11     int size()返回当前集合中元素的数量
12     
13     boolean contains(Object o)查找集合中是否有指定的对象
14     boolean isEmpty()判断集合是否为空
15     Iterator iterator()返回一个迭代器
16     
17     boolean containsAll(Collection c)查找集合中是否有集合c中的元素
18     boolean addAll(Collection c)将集合c中所有的元素添加给该集合
19     void removeAll(Collection c)从集合中删除c集合中也有的元素
20     void retainAll(Collection c)从集合中删除集合c中不包含的元素
21  * 
22  * 三、主要子接口对象两个------list set!
23  */
package cn.zhou.com;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/*
 * 1.在集合中储存自定义的对象。
 * 
 * 2.创建集合对象
 * 
 * 3.添加具体的学生元素!
 * 
 * 
 */
public class CelloctionDemo003 {
	public static void main(String[] args) {
		//创建集合对象
		Collection coll=new ArrayList();
		
		//添加具体的学生对象!
		Student stu=new Student("张三",22);
			coll.add(stu);
			coll.add(new Student("李四",55));
			
			//使用具体的迭代器对象 获取集合元素
			for (Iterator it = coll.iterator(); it.hasNext();) {
				
				Student student = (Student) it.next();//
				
		
				
				System.out.println(student.getName());
			//	System.out.println(student.);
				
			}
	}
}

  

 
原文地址:https://www.cnblogs.com/ZXF6/p/10571663.html