Java——集合(Collection接口),迭代器,加強for循環,泛型

1、集合

Collection接口中的方法是集合中全部實現類必須擁有的方法。html

  • ArrayList implements List
  • List extends Collection

一、基本使用

其餘java

import java.util.ArrayList;
/*
 *  集合體系,
 *    目標  集合自己是一個存儲的容器:
 *       必須使用集合存儲對象
 *       遍歷集合,取出對象
 *       集合本身的特性
 */
public class ArrayListDemo {
	public static void main(String[] args) {
		/*
		 *  集合ArrayList,存儲int類型數
		 *  集合自己不接受基本類,自動裝箱存儲
		 */
		ArrayList<Integer> array = new ArrayList<Integer>();
		array.add(11);
		array.add(12);
		for(int i = 0 ; i < array.size() ;i++){
			System.out.println(array.get(i));
		}
		/*
		 *  集合存儲自定義的Person類的對象
		 */
		ArrayList<Person> arrayPer = new ArrayList<Person>();
		arrayPer.add(new Person("a",20));
		arrayPer.add(new Person("b",18));
		arrayPer.add(new Person("c",22));
		for(int i = 0 ; i < arrayPer.size();i++){
			//get(0),取出的對象Person對象
			//打印的是一個對象,必須調用的toString()
			System.out.println(arrayPer.get(i));
		}
	}
}

二、清空集合中的元素

/*
 * Collection接口的方法
 * void clear() 清空集合中的全部元素
 * 集合容器自己依然存在
 */
public static void function(){
	//接口多態的方式調用
	Collection<String> coll = new ArrayList<String>();
	coll.add("abc");
	coll.add("bcd");
	System.out.println(coll);  // ["abc","bcd"]

	coll.clear();

	System.out.println(coll);

}

三、判斷對象是否存在於集合中

/*
 * Collection接口方法
 * boolean contains(Object o) 判斷對象是否存在於集合中,對象存在返回true
 * 方法參數是Object類型
 */
private static void function_1() {
	Collection<String> coll = new ArrayList<String>();
	coll.add("abc");
	coll.add("money");
	coll.add("123");
	
	boolean b = coll.contains("abc");
	System.out.println(b);
}

四、將集合轉成數組

/*  Collection接口方法
 *  Object[] toArray() 集合中的元素,轉成一個數組中的元素, 集合轉成數組
 *  返回是一個存儲對象的數組, 數組存儲的數據類型是Object
 */
private static void function_2() {
	Collection<String> coll = new ArrayList<String>();
	coll.add("abc");
	coll.add("itcast");
	coll.add("itheima");
	coll.add("money");
	coll.add("123");
	
	Object[] objs = coll.toArray();
	for(int i = 0 ; i < objs.length ; i++){
		System.out.println(objs[i]);
	}
}

五、移除元素

/*
 * Collection接口方法
 * boolean remove(Object o)移除集合中指定的元素
 */
private static void function_3(){
	Collection<String> coll = new ArrayList<String>();
	coll.add("abc");
	coll.add("money");
	coll.add("money");
	coll.add("123");	
	System.out.println(coll);
	
	boolean b = coll.remove("money");  //只會移除第一個
	System.out.println(b);
	System.out.println(coll);
}

2、迭代器

迭代器是一種設計模式,它是一個對象,它能夠遍歷並選擇序列中的對象,而開發人員不須要了解該序列的底層結構。迭代器一般被稱爲「輕量級」對象,由於建立它的代價小。設計模式

集合中的迭代器

因爲Java中有多種不一樣的集合,咱們又想有一個統一的方式來獲取它的值,因此有了迭代器。數組

接口Iterator : 其中有兩個抽象方法安全

  • boolean hasNext() 判斷集合中還有沒有能夠被取出的元素,若是有返回true
  • next() 取出集合中的下一個元素

ArrayList重寫方法 iterator(),返回了Iterator接口的實現類的對象spa

Iterator it = array.iterator(),運行結果就是Iterator接口的實現類的對象設計

it是接口的實現類對象,調用方法 hasNext 和 next 集合元素迭代code

一、基本使用

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

public class CollectionDemo1 {
	public static void main(String[] args) {
		//集合能夠存儲任意類型的對象
		//集合中,不指定存儲的數據類型, 集合就能夠什麼都存
		Collection coll = new ArrayList();  // 多態
		coll.add("abc");
		coll.add("uyjgtfd");
		
		//迭代器獲取
		Iterator it = coll.iterator();
		while(it.hasNext()){
			//it.next()獲取出來的是什麼數據類型,Object類
			//Object obj = it.next();
			//System.out.println(obj);
			String s = (String)it.next();
			System.out.println(s.length());
		}
	}
}

二、指定迭代數據類型

public static void main(String[] args) {
	Collection<String> coll = new ArrayList<String>();
	coll.add("abc1");
	coll.add("abc2");
	coll.add("abc3");
	coll.add("abc4");
	//迭代器,對集合ArrayList中的元素進行取出
	
	//調用集合的方法iterator()獲取出,Iterator接口的實現類的對象
	Iterator<String> it = coll.iterator();
	//接口實現類對象,調用方法hasNext()判斷集合中是否有元素
	//boolean b = it.hasNext();
	//System.out.println(b);
	//接口的實現類對象,調用方法next()取出集合中的元素
	//String s = it.next();
	//System.out.println(s);
	
	//迭代是反覆內容,使用循環實現,循環的條件,集合中沒元素, hasNext()返回了false
	while(it.hasNext()){
		String s = it.next();
		System.out.println(s);
	}
	
	/*for (Iterator<String> it2 = coll.iterator(); it2.hasNext();  ) {  // for循環實現迭代器
		System.out.println(it2.next());
	}*/
	
}

3、加強for循環

JDK1.5新特性,加強for循環htm

JDK1.5版本後,出現新的接口 java.lang.Iterable,Collection接口是繼承Iterable對象

加強for循環格式:

for( 數據類型 變量名 : 數組或者集合 ){

  sout(變量);

}

/*
 *  加強for循環遍歷集合
 *  存儲自定義Person類型
 */
public static void function_2(){
	ArrayList<Person> array = new ArrayList<Person>();
	array.add(new Person("a",20));
	array.add(new Person("b",10));
	for(Person p : array){
		System.out.println(p);
	}
}


public static void function_1(){
	//for對於對象數組遍歷的時候,可否調用對象的方法呢?固然能,s就是遍歷出的對象
	String[] str = {"abc","itcast","cn"};
	for(String s : str){
		System.out.println(s.length());
	}
}

/*
 *  實現for循環,遍歷數組
 *  好處: 代碼少了,方便對容器遍歷
 *  弊端: 沒有索引,不能操做容器裏面的元素
 */
public static void function(){
	int[] arr = {3,1,9,0};
	for(int i : arr){
		System.out.println(i+1);
	}
	System.out.println(arr[0]);
}

4、泛型

泛型是JDK1.5出現新的安全機制,保證程序的安全性

  • 泛型: 指明瞭集合中存儲數據的類型 <數據類型>
public static void function(){
	Collection<String> coll = new ArrayList<String>();
	coll.add("abc");
	coll.add("rtyg");
	coll.add("43rt5yhju");
	//coll.add(1);
	
	Iterator<String> it = coll.iterator();
	while(it.hasNext()){
		String s = it.next();
		System.out.println(s.length());
	}
}

一、帶有泛型的類

/*
 *  帶有泛型的類
 *    ArrayList
 *    E: Element 元素, 實際思想就是一個變量而已 
 *    ArrayList<Integer> , E 接受到類型,就是Integer類型
 *    public class ArrayList<E>{
 *    
 *       public boolean add(Integer e){
 *          elementData[size++] = e;
 *       }
 *       
 *       public boolean add(E e){}
 *    }
 *    
 *    Iterator<E> 
 *    E next()
 *    
 *    Iterator<Integer> 
 *    Integer next()
 *   
 */
public class GenericDemo1 {
	public static void main(String[] args) {
		ArrayList<Integer>  array = new ArrayList<Integer> ();
		array.add(123);
		array.add(456);
		// ArrayList集合,本身有個方法
		// <T> T[] toArray(T[] a) 
		Integer[] i = new Integer[array.size()];
		Integer [] j = array.toArray(i);
		for(Integer k : j){
			System.out.println(k);
		}

	}
}

二、帶有泛型的接口

public static void function(){
	Collection<String> coll = new ArrayList<String>();
	coll.add("abc");
	coll.add("rtyg");
	coll.add("43rt5yhju");
	//coll.add(1);
	
	Iterator<String> it = coll.iterator();
	while(it.hasNext()){
		String s = it.next();
		System.out.println(s.length());
	}
}

三、泛型的通配符

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

/*
 *  泛型的通配符
 */
public class GenericDemo {
	public static void main(String[] args) {
		ArrayList<String> array = new ArrayList<String>();
		
		HashSet<Integer> set = new HashSet<Integer>();
		
		array.add("123");
		array.add("456");
		
		set.add(789);
		set.add(890);
		
		iterator(array);
		iterator(set);
	}
	/*
	 *  定義方法,能夠同時迭代2個集合
	 *  參數: 怎麼實現 , 不能寫ArrayList,也不能寫HashSet
	 *  參數: 或者共同實現的接口
	 *  泛型的通配,匹配全部的數據類型  ?
	 */
	public static void iterator(Collection<?> coll){
		Iterator<?> it = coll.iterator();
		while(it.hasNext()){
			//it.next()獲取的對象,什麼類型
			System.out.println(it.next());
		}
	}
}

四、示例

/*
 *  將的酒店員工,廚師,服務員,經理,分別存儲到3個集合中
 *  定義方法,能夠同時遍歷3集合,遍歷三個集合的同時,能夠調用工做方法
 */
import java.util.ArrayList;
import java.util.Iterator;
public class GenericTest {
	public static void main(String[] args) {
		//建立3個集合對象
		ArrayList<ChuShi> cs = new ArrayList<ChuShi>();
		ArrayList<FuWuYuan> fwy = new ArrayList<FuWuYuan>();
		ArrayList<JingLi> jl = new ArrayList<JingLi>();
		
		//每一個集合存儲本身的元素
		cs.add(new ChuShi("張三", "後廚001"));
		cs.add(new ChuShi("李四", "後廚002"));
		
		fwy.add(new FuWuYuan("翠花", "服務部001"));
		fwy.add(new FuWuYuan("酸菜", "服務部002"));
		
		jl.add(new JingLi("小名", "董事會001", 123456789.32));
		jl.add(new JingLi("小強", "董事會002", 123456789.33));
		
//		ArrayList<String> arrayString = new ArrayList<String>();
		iterator(jl);
		iterator(fwy);
		iterator(cs);
	
	}
	/*
	 * 定義方法,能夠同時遍歷3集合,遍歷三個集合的同時,能夠調用工做方法 work
	 * ? 通配符,迭代器it.next()方法取出來的是Object類型,怎麼調用work方法
	 * 強制轉換:  it.next()=Object o ==> Employee
	 * 方法參數: 控制,能夠傳遞Employee對象,也能夠傳遞Employee的子類的對象
	 * 泛型的限定  本案例,父類固定Employee,可是子類能夠無限?
	 *   ? extends Employee 限制的是父類, 上限限定, 能夠傳遞Employee,傳遞他的子類對象
	 *   ? super   Employee 限制的是子類, 下限限定, 能夠傳遞Employee,傳遞他的父類對象
	 */
	public static void iterator(ArrayList<? extends Employee> array){
		
		 Iterator<? extends Employee> it = array.iterator();
		 while(it.hasNext()){
			 //獲取出的next() 數據類型,是什麼Employee
			 Employee e = it.next();
			 e.work();
		 }
	}
}
相關文章
相關標籤/搜索