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.);
				
			}
	}
}
 
         

  

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM