詳解JAVA中的for-each循環(huán)與迭代
在學習java中的collection時注意到,collection層次的根接口Collection實現了Iterable<T>接口(位于java.lang包中),實現這個接口允許對象成為 "foreach" 語句的目標,而此接口中的唯一方法,實現的就是返回一個在一組 T 類型的元素上進行迭代的迭代器。
一、迭代器Iterator
接口:Iterator<T>
public interface Iterator<E>{
boolean hasNext();
E next();
void remove();
}
查看Iterator接口API可以知道,這是對collection進行迭代的迭代器。迭代器允許調用者利用定義良好的語義在迭代期間從迭代器所指向的 collection 移除元素。
尤其值得注意的是此迭代器remove()方法的使用:從迭代器指向的 collection 中移除迭代器返回的最后一個元素(可選操作)。每次調用 next 只能調用一次此方法。如果進行迭代時用調用此方法(remove方法)之外的其他方式修改了該迭代器所指向的 collection,則迭代器的行為是不確定的。 接口設計人員在設計Iterator<T>接口的時候已經指出,在進行迭代時如果調用了除了迭代器的remove()方法修改了該迭代器所指向的collection,則會造成不確定的后果。具體出現什么后果依迭代器的具體實現而定。針對這種不確定的后果可能出現的情況,在學習ArrayList時遇到了其中一種:迭代器拋出 ConcurrentModificationException異常。具體異常情況如下代碼所示:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class ItaratorTest {
public static void main(String[] args) {
Collection<String> list = new ArrayList<String>();
list.add("Android");
list.add("IOS");
list.add("Windows Mobile");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String lang = iterator.next();
list.remove(lang);//will throw ConcurrentModificationException
}
}
}
此段代碼在運行時會拋出ConcurrentModificationException異常,因為我們在迭代器運行期間沒有用iterator的remove()方法來刪除元素,而是使用ArrayList的 remove()方法改變了迭代器所指向的collection。這就違反了迭代器的設計原則,所以發(fā)生了異常。
所報異常情況如下所示:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
at java.util.ArrayList$Itr.next(ArrayList.java:831)
at Text.ItaratorTest.main(ItaratorTest.java:17)
二、for-each循環(huán)與迭代器Iterator<T>
從Java5起,在Java中有了for-each循環(huán),可以用來循環(huán)遍歷collection和array。Foreach循環(huán)允許你在無需保持傳統(tǒng)for循環(huán)中的索引,或在使用iterator /ListIterator(ArrayList中的一種迭代器實現)時無需調用while循環(huán)中的hasNext()方法就能遍歷collection。for-each循環(huán)簡化了任何Collection或array的遍歷過程。但是使用foreach循環(huán)也有兩點需要注意。
使用foreach循環(huán)的對象,必須實現了Iterable<T>接口
請看如下示例:
import java.util.ArrayList;
public class ForeachTest1 {
public static void main(String args[]) {
CustomCollection<String> myCollection = new CustomCollection<String>();
myCollection.add("Java");
myCollection.add("Scala");
myCollection.add("Groovy");
// What does this code will do, print language, throw exception or
// compile time error
for (String language : myCollection) {
System.out.println(language);
}
}
private class CustomCollection<T> {
private ArrayList<T> bucket;
public CustomCollection() {
bucket = new ArrayList();
}
public int size() {
return bucket.size();
}
public boolean isEmpty() {
return bucket.isEmpty();
}
public boolean contains(T o) {
return bucket.contains(o);
}
public boolean add(T e) {
return bucket.add(e);
}
public boolean remove(T o) {
return bucket.remove(o);
}
}
}
上述代碼將無法通過編譯,這是因為代碼中的CustomCollection類沒有實現Iterable<T>接口,編譯期的報錯如下:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Can only iterate over an array or an instance of java.lang.Iterable
at Text.ForeachTest1.main(ForeachTest1.java:15)
事實上,無需等到編譯時才發(fā)現報錯,eclipse會在這段代碼寫完之后就會在foreach循環(huán)處顯示錯誤:Can only iterate over an array or an instance of java.lang.Iterable
從上述示例可以再次得到確認的是,foreach循環(huán)只適用于實現了Iterable<T>接口的對象。由于所有內置Collection類都實現了java.util.Collection接口,已經繼承了Iterable,所以為了解決上述問題,可以選擇簡單地讓CustomCollection實現Collection接口或者繼承AbstractCollection。解決方式如下:
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Iterator;
public class ForeachTest {
public static void main(String args[]) {
CustomCollection<String> myCollection = new CustomCollection<String>();
myCollection.add("Java");
myCollection.add("Scala");
myCollection.add("Groovy");
for (String language : myCollection) {
System.out.println(language);
}
}
private static class CustomCollection<T> extends AbstractCollection<T> {
private ArrayList<T> bucket;
public CustomCollection() {
bucket = new ArrayList();
}
public int size() {
return bucket.size();
}
public boolean isEmpty() {
return bucket.isEmpty();
}
public boolean contains(Object o) {
return bucket.contains(o);
}
public boolean add(T e) {
return bucket.add(e);
}
public boolean remove(Object o) {
return bucket.remove(o);
}
@Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return bucket.iterator();
}
}
}
2.foreach循環(huán)的內部實現也是依靠Iterator進行實現的
為了驗證foreach循環(huán)是使用Iterator作為內部實現這一事實,我們依然采用本文最開始的實例進行驗證:
public class ItaratorTest {
public static void main(String[] args) {
Collection<String> list = new ArrayList<String>();
list.add("Android");
list.add("IOS");
list.add("Windows Mobile");
// example1
// Iterator<String> iterator = list.iterator();
// while (iterator.hasNext()) {
// String lang = iterator.next();
// list.remove(lang);
// }
// example 2
for (String language : list) {
list.remove(language);
}
}
}
程序運行時所報異常:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
at java.util.ArrayList$Itr.next(ArrayList.java:831)
at Text.ItaratorTest.main(ItaratorTest.java:22)
此異常正說明了for-each循環(huán)內部使用了Iterator來遍歷Collection,它也調用了Iterator.next(),這會檢查(元素的)變化并拋出ConcurrentModificationException。
總結:
- 在遍歷collection時,如果要在遍歷期間修改collection,則必須通過Iterator/listIterator來實現,否則可能會發(fā)生“不確定的后果”。
- foreach循環(huán)通過iterator實現,使用foreach循環(huán)的對象必須實現Iterable接口
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java中一維二維數組的靜態(tài)和動態(tài)初始化
今天通過本文給大家分享Java中的數組,包括一維數組和二維數組的靜態(tài)初始化和動態(tài)初始化問題,感興趣的朋友一起看看吧2017-10-10
Java并發(fā)編程之volatile與JMM多線程內存模型
這篇文章主要介紹了Java并發(fā)volatile與JMM多線程內存模型,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
解決使用@Value(${×××))從properties文件取值的坑
這篇文章主要介紹了解決使用@Value(${×××))從properties文件取值的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot集成Hadoop對HDFS的文件操作方法
這篇文章主要介紹了SpringBoot集成Hadoop對HDFS的文件操作方法,本文給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-07-07

