国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Iterator迭代器對象

2019-11-14 22:56:42
字體:
來源:轉載
供稿:網友
Iterator迭代器對象

目錄:

》迭代器Iterator的使用

》迭代字符串集合

》迭代對象集合

》迭代器使用圖解,和原理分析

java迭代器源代碼

》迭代器Iterator的使用:

》迭代字符串集合

import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;/** * 集合的迭代 *  * Iterator iterator() 集合的專用迭代器 *      E next()獲取指針當前的元素,指針移向下一個元素 *             NoSuchElementException 沒有這樣的元素,因為你已經找到了最后 *         boolean hasNext() 如果仍有元素可以迭代,則返回 true。 */public class IteratorDemo {    public static void main(String[] args) {        //創建集合        Collection c=new ArrayList();                //向集合中添加元素        c.add("hello");        c.add("world");        c.add("java");                //Iterator iterator()迭代器,集合的專用迭代方式        Iterator it=c.iterator();//Iterator是接口,iterator() 方法返回的是實現類,這里是多態        System.out.PRintln(it.next());        System.out.println(it.next());        System.out.println(it.next());//        System.out.println(it.next());//                        //        String s=null;//        while(it.hasNext()){//            s=(String)it.next();//            System.out.println(s);//        }    }}

》迭代對象集合

import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;/** *  * 練習:用集合存儲5個學生對象,并把學生進行遍歷,用迭代器遍歷。 * *問題1:能用while循環,那么能不能用for循環? *問題2:不要次使用it.next()方法,因為每次使用都是訪問一個對象。 */public class IteratorTest {    public static void main(String[] args) {        //創建集合對象        Collection c=new ArrayList();                //創建學生對象        Student s1=new Student("林清",26);        Student s2=new Student("周潤發",45);        Student s3=new Student("黃健翔",25);        Student s4=new Student("謝霆鋒",30);        Student s5=new Student("王菲",30);                //向集合中添加學生對象        c.add(s1);        c.add(s2);        c.add(s3);        c.add(s4);        c.add(s5);                //得到集合的迭代器        Iterator it=c.iterator();                //使用迭代器遍歷學生集合        Student s=null;        while(it.hasNext()){             s=(Student)it.next();             System.out.println(s.getName()+"------"+s.getAge());                          //NoSuchElementException 不要多次使用it.next()方法            // System.out.println(((Student)it.next()).getName()+"-----"+((Student)it.next()).getAge());        }                        //for循環改寫    /*    Student s=null;        for(Iterator it=c.iterator();it.hasNext();){             s=(Student)it.next();             System.out.println(s.getName()+"------"+s.getAge());        }*/    }}

 

//bean

public class Student {    private String name;    private int age;            public Student() {        super();    }    public Student(String name, int age) {        super();        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    }

 

》迭代器使用圖解,和原理分析

》java迭代器源代碼

public interface Inteator {    boolean hasNext();    Object next(); }public interface Iterable {    Iterator iterator();}public interface Collection extends Iterable {    Iterator iterator();}public interface List extends Collection {    Iterator iterator();}public class ArrayList implements List {//在實現類里面才有Iterator接口的具體實現    public Iterator iterator() {        return new Itr();    }        private class Itr implements Iterator {//是一個內部類,并且是不為外人所知的私有的        public boolean hasNext() {}        public Object next(){}     }}Collection c = new ArrayList();c.add("hello");c.add("world");c.add("java");Iterator it = c.iterator();     //new Itr();while(it.hasNext()) {    String s = (String)it.next();    System.out.println(s);}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临沂市| 普安县| 泰兴市| 阿克陶县| 二手房| 柞水县| 沙田区| 彰化市| 龙海市| 定兴县| 乐安县| 乌兰县| 乌海市| 景东| 郧西县| 冷水江市| 涟水县| 天台县| 全南县| 湖州市| 渭南市| 温宿县| 阿勒泰市| 宜丰县| 都匀市| 平远县| 晋中市| 浙江省| 巫山县| 安泽县| 隆化县| 忻州市| 广平县| 北辰区| 闻喜县| 固原市| 清徐县| 泉州市| 兴文县| 石门县| 城固县|