java基础-迭代器(Iterator)与增强for循环
java基礎-迭代器(Iterator)與增強for循環
作者:尹正杰
版權聲明:原創作品,謝絕轉載!否則將追究法律責任。
?
?
一.Iterator迭代器概述
Java中提供了很多個集合,它們在存儲元素時,采用的存儲方式不同。我們需要取出這些集合中的元素,可通過一種通用的方式來完成。
Collection集合元素的通用獲取方式:在取元素之前先要判斷集合中有沒有元素,如果有,就把這個元素取出來,繼續在判斷,如果還有就在取出來。一直把集合中所有的元素全部取出。這種取出方式專業術語稱為迭代。換句話說,迭代是取出集合中元素的一種方式,因為Collection中有iterator方法,所以每一個子類集合對象都是迭代器對象。
?
二.迭代器的實現原理
其實集合中的迭代器就是獲取結合中元素的方式。這是時候我們就不得不說一下Iterator接口了,經過查閱API文檔發現它有三個抽象方法,如下:
由于Iterator只是接口,不能被直接實例化,因此想要使用該接口就必須實現該接口的所有抽象方法,而我們學習的Collection接口也定影實現Iterator接口的抽象方法,即“iterator()”。因此只要是Collection的實現類就必須重寫“iterator()”方法,最終返回“Iterator”接口實現類的對象(也就是可迭代對象),然后在調用hasNext()和next()方法來對集合進行迭代操作。
綜上所述,我們可以總結為以下三點:
1>.迭代器不保證取出來元素的順序和存入的順序一致,“有序”是靠集合實例本身保證的;
2>.迭代器本身是一個接口,該方法返回的是一個迭代器實例對象,通常使用的是接口多態使用迭代器;
3>.迭代器中常用的兩個方法是:
a>.boolean? ? hasNext() :? 用來判斷結合中是否有下一個元素可以迭代,如果返回true,說明可以迭代。
b>.Object? ? ? next() :? ?用來返回迭代的下一個元素,并把指針向后移動一位。
迭代器的執行過程,可以用張圖來幫助大家理解,如下:
三.迭代器的代碼實現
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.note; 8 9 import java.util.ArrayList; 10 import java.util.Collection; 11 import java.util.Iterator; 12 13 public class IteratorDemo { 14 public static void main(String[] args) { 15 Collection<String> coll = new ArrayList<String>(); 16 coll.add("yinzhengjie"); 17 coll.add("尹正杰"); 18 coll.add("java"); 19 coll.add("python"); 20 coll.add("shell"); 21 coll.add("golang"); 22 23 //調用集合的方法iterator()獲取出,Iterator接口的實現類的對象 24 Iterator<String> it1 = coll.iterator(); 25 26 System.out.println("第一種方式進行迭代:"); 27 //1>.用while循環遍歷集合進行迭代 28 while(it1.hasNext()) { 29 System.out.println("\t"+it1.next()); 30 } 31 32 System.out.println("第二種方式進行迭代:"); 33 //2>.用for循環進行迭代(相對while循環更節省內存,因為it2是for局部遍歷,而it1是main方法的變量) 34 for(Iterator<String> it2 = coll.iterator();it2.hasNext();) { 35 System.out.println("\t"+it2.next()); 36 } 37 38 System.out.println("第三種方式進行迭代:"); 39 //3>.用foreach循環(也叫增強for循環)繼續迭代,需要JDK1.5版本以后才可以喲! 40 for (String string : coll) { 41 System.out.println("\t"+string); 42 } 43 } 44 } 45 46 47 /* 48 以上代碼執行結果如下: 49 第一種方式進行迭代: 50 yinzhengjie 51 尹正杰 52 java 53 python 54 shell 55 golang 56 第二種方式進行迭代: 57 yinzhengjie 58 尹正杰 59 java 60 python 61 shell 62 golang 63 第三種方式進行迭代: 64 yinzhengjie 65 尹正杰 66 java 67 python 68 shell 69 golang 70 */?
四.集合迭代過程中的轉型
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.note; 8 9 import java.util.ArrayList; 10 import java.util.Collection; 11 import java.util.Iterator; 12 13 public class IteratorDemo { 14 public static void main(String[] args) { 15 Collection<String> coll = new ArrayList<String>(); 16 coll.add("yinzhengjie"); 17 coll.add("尹正杰"); 18 coll.add("Java"); 19 20 Iterator it = coll.iterator(); 21 while(it.hasNext()){ 22 Object obj = it.next(); 23 if(obj instanceof String) { 24 String str = (String)obj; 25 System.out.println(str.length()); 26 } 27 } 28 } 29 } 30 31 32 /* 33 以上代碼執行結果如下: 34 11 35 3 36 4 37 */?
五.增強for循環遍歷數組
增強for循環是JDK1.5以后出來的一個高級for循環,專門用來遍歷數組和集合的,它的內部原理其實是一個Iterator迭代器,所以在遍歷的過程中,不能對集合中的元素進行增刪操作。JDK1.5版本后,出現新的接口,即“java.lang.Iterable”。只要實現“java.lang.Iterable”這個接口,就允許對象稱為增強for循環(“foreach”語句,注意“foreach”并不是關鍵字喲!)的目標。
1 格式如下: 2 for( 數據類型 變量名:數組或這集合){ 3 System.out.println(變量名); 4 }如果只做遍歷的話,推薦大家使用增強for循環, 因為for循環存在優點的同時也會存在缺點。
優點:代碼少了,方便對容器遍歷。
缺點:沒有索引,不能操作容器里面的元素。
接下來我們用foreach來遍歷數組和集合,代碼如下:
1>.遍歷數組
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.note; 8 9 public class ForeachDemo { 10 public static void main(String[] args) { 11 12 String[] arr = {"yinzhengjie","org","cn"}; 13 for (String string : arr) { 14 System.out.println(string+"\t\t"+string.length()); 15 } 16 } 17 } 18 19 20 /* 21 以上代碼執行結果如下: 22 yinzhengjie 11 23 org 3 24 cn 2 25 */2>.遍歷集合
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.note; 8 9 import java.util.ArrayList; 10 import java.util.Collection; 11 12 class Student{ 13 private String name; 14 private int age; 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public int getAge() { 22 return age; 23 } 24 public void setAge(int age) { 25 this.age = age; 26 } 27 public Student(String name, int age) { 28 super(); 29 this.name = name; 30 this.age = age; 31 } 32 public Student() { 33 super(); 34 } 35 //這里我們重寫一下toString方法!方便System.out.pirntln在調用該對象的toString方法時不會去找Object的默認返回值。 36 public String toString() { 37 return this.name + "---" + this.age; 38 39 } 40 } 41 42 43 public class ForeachDemo { 44 public static void main(String[] args) { 45 //接口多態 46 Collection<Student> coll = new ArrayList<>(); 47 coll.add(new Student("yinzhengjie",18)); 48 coll.add(new Student("尹正杰",20)); 49 50 for (Student p : coll) { 51 System.out.println(p); 52 } 53 } 54 } 55 56 57 /* 58 以上代碼執行結果如下: 59 yinzhengjie 11 60 org 3 61 cn 2 62 */?
總結
以上是生活随笔為你收集整理的java基础-迭代器(Iterator)与增强for循环的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Javascript Symbol 隐匿
- 下一篇: rocketMq - commitLog