Java 从入门到进阶之路(二十二)


在之前的文章我们介绍了一下 Java 中的  集合框架中的Collection 中的一些常用方法,本章我们来看一下 Java 集合框架中的Collection 的迭代器 Iterator。

当我们创建完成集合后,怎么样从集合中取元素呢?Java 为我们提供了迭代器 Iterator 来帮我们实现,如下:

 1 import java.util.ArrayList;
 2 import java.util.Collection;
 3 import java.util.Iterator;
 4 
 5 /**
 6  * 遍历集合
 7  * Collection 提供了统一的遍历集合元素的方式:迭代器模式
 8  * Iterator iterator() 获取用于遍历当前集合的迭代器
 9  * java.util.Iterator 是一个接口,规定了用于遍历集合元素的相关方法,
10  * 不同的集合提供了相应的实现类,
11  * 无需记住那些类的名字,只将他们当做 Iterator 即可
12  * 

13 * 遍历集合遵循:问,取,删的步骤,其中删除不是必须操作 14 */ 15 public class Main { 16 public static void main(String[] args) { 17 Collection collection = new ArrayList(); 18 collection.add("one"); 19 collection.add("two"); 20 collection.add("three"); 21 collection.add("four"); 22 // 获取用于遍历当前集合的迭代器 23 Iterator iterator = collection.iterator(); 24 /** 25 * boolean hasNext() 问的过程 26 * 该方法是判断集合中是否还有元素可以取出 27 * 28 * E next() 取的过程 29 * 获取集合中下一个元素 30 * */ 31 while (iterator.hasNext()) { 32 String string = (String) iterator.next(); 33 System.out.println(string); // one two three four 34 } 35 } 36 }

从上面的代码可以看出,我们可以通过迭代器 Iterator 来输出每一个元素,那如果我们想要删除其中一个元素呢,既然我们可以遍历出每个元素,那我们可不可以通过 equals 来判断,如果存在就通过之前讲的 remove 方法删除掉呢,如下:

 1 import java.util.ArrayList;
 2 import java.util.Collection;
 3 import java.util.Iterator;
 4 
 5 public class Main {
 6     public static void main(String[] args) {
 7         Collection collection = new ArrayList();
 8         collection.add("one");
 9         collection.add("two");
10         collection.add("three");
11         collection.add("four");
12         // 获取用于遍历当前集合的迭代器
13         Iterator iterator = collection.iterator();
14         while (iterator.hasNext()) {
15             String string = (String) iterator.next();
16             if ("one".equals(string)) {
17                 /**
18                  * 在使用迭代器遍历集合时,不要使用集合的方法增删改查
19                  * 否则会引发异常
20                  * */
21                 // collection.remove(string); // 编译器错误 Exception in thread "main" java.util.ConcurrentModificationException
22                 iterator.remove(); // 可以直接调用 Iterator 自带的 remove 方法,相当于去一个删一个
23             }
24         }
25         System.out.println(collection); // [two, three, four]
26     }
27 }

我们在讲数组的时候,对数组进行过遍历操作,并且用到过 for each 方法,这个在集合中同样适用,如下:

 1 import java.util.ArrayList;
 2 import java.util.Collection;
 3 
 4 /**
 5  * JDK5.0 之后推出了一个新的特性
 6  * 增强 for 循环,又叫做新循环,for each
 7  * 

8 * 新循环不能代替传统循环, 9 * 作用仅仅是用来遍历集合或数组的 10 */ 11 12 public class Main { 13 public static void main(String[] args) { 14 String[] array = {"one", "two", "three", "four"}; 15 for (int i = 0; i < array.length; i++) { 16 System.out.println(array[i]); // one two three four 17 } 18 for (String string : array) { 19 System.out.println(string); // one two three four 20 } 21 22 Collection collection = new ArrayList(); 23 collection.add("one"); 24 collection.add("two"); 25 collection.add("three"); 26 collection.add("four"); 27 for (Object object : collection) { 28 String string = (String) object; 29 System.out.println(string); // one two three four 30 } 31 } 32 }

在上面的代码中,可以看出我们可以通过 for each 的形式输出 collection 集合,那可不可以通过 collection.remove() 进行删除呢,如下:

 1 import java.util.ArrayList;
 2 import java.util.Collection;
 3 
 4 public class Main {
 5     public static void main(String[] args) {
 6         Collection collection = new ArrayList();
 7         collection.add("one");
 8         collection.add("two");
 9         collection.add("three");
10         collection.add("four");
11         for (Object object : collection) {
12             String string = (String) object;
13             if("one".equals(string)){
14                 /**
15                  * 新循环并非新的语法,
16                  * 新循环是编译器认可,而不是虚拟机认可
17                  * 使用新循环遍历时,编译器会将它改为迭代器但是遍历
18                  * 所以在使用新循环遍历集合时,不能通过集合的方法增删改查
19                  * */
20                 collection.remove(string); // 编译错误 Exception in thread "main" java.util.ConcurrentModificationException
21             }
22         }
23     }
24 }

新循环并不能实现我们想要的增删改查,我们可以简单低理解为旧瓶装新酒。