(JAVA)CollectionDemo3
生活随笔
收集整理的這篇文章主要介紹了
(JAVA)CollectionDemo3
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
package cn.cast.collection;import com.sun.tools.jdi.EventSetImpl;import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;/*** @author zhangyu* @date 2021年08月24日 11:40 下午* 獲取Collectuon 接口有一個方法* Iterator iterator()* 返回值是一個接口類型* boolean hasNext():判斷集合中有沒有下一個被取出的元素* Object next()獲取集集合下一個元素* 1.iterator是所有集合都有的一個方法* 2.迭代器是獲取集合中儲存對象的方式* publish class ArrayList implement Collection{* publish Iterator iterator(){* //返回Iterator接口實現(xiàn)的對象* return new Itr();* }* //定義內(nèi)部類* private class Itr implements Iterator{* public boolean hasNext(){* return false;* }* public Object next(){* return null;* }* public void remove(){** }** }* }* main(){* Collection col = new ArrayList();* Iterator it = col.iterator();* it.hasNext();* }**/
public class CollectionDemo2 {public static void main(String[] args) {method_1();method_2();}public static void method_1(){Collection col = new ArrayList();col.add("abc1");col.add("zhangsan ");col.add(123);Iterator it = col.iterator();//hasNest()判斷迭代器內(nèi)容是否為空while (it.hasNext()){Object o = it.next();System.out.println(o);}}public static void method_2(){Collection col = new ArrayList();col.add("abc1");col.add("zhangsan ");col.add(123);//for循環(huán)要求,冒號中間值為booblean即可,前后可隨意for (Iterator it = col.iterator(); it.hasNext();){Object o = it.next();System.out.println(o);}}
}
總結(jié)
以上是生活随笔為你收集整理的(JAVA)CollectionDemo3的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何通过windows控制linux,如
- 下一篇: js取色器的使用