0911周四

来源:互联网 发布:安卓侧滑栏源码 编辑:程序博客网 时间:2024/04/29 13:15

不使用 Iterator

使用 for/in 与“普通”for 之间的最基本区别是,您不必使用计数器(通常称为 icount)或 Iterator。参见清单 1,它显示了一个使用的 Iteratorfor 循环:


清单 1. for 循环,旧式学院风格
        public void testForLoop(PrintStream out) throws IOException {  List list = getList();  // initialize this list elsewhere    for (Iterator i = list.iterator(); i.hasNext(); ) {    Object listElement = i.next();    out.println(listElement.toString());        // Do something else with this list element  }}      

清单 2. 转换成 for/in
        public void testForInLoop(PrintStream out) throws IOException {  List list = getList();  // initialize this list elsewhere    for (Object listElement : list) {    out.println(listElement.toString());        // Do something else with this list element  }}      

清单 3. for/in 循环的基本结构

for(
        声明
        表达式

        语句

清单 4. 转换后的 for/in 循环,带有一个 Iterable

        for (Iterator<        E> #i = (        expression).iterator(); #i.hasNext(); ) {         declaration = #i.next();         statement}清单 5. 转换后的 for/in 循环,没有未经参数化的类型
        for (Iterator #i = (        expression).iterator(); #i.hasNext(); ) {         declaration = #i.next();         statement}      

使用数组

现在您已经了解了基本的语义,可以继续了解一些更具体的示例了。您已经看到 for/in 如何处理列表了;处理数组也一样容易。与集合相同,数组也被赋值(如清单 6 所示),然后这些值被逐个取出,并被处理。


清单 6. 简单的数组初始化
        int[] int_array = new int[4];String[] args = new String[10];float[] float_array = new float[20];      

对于使用 for 以及计算器或索引变量的场合,现在就可以使用 for/in(当然,前提是您正在使用 Tiger)。清单 7 显示了另外一个简单的示例:


清单 7. 用 for/in 对数组进行循环就是小菜一碟
        public void testArrayLooping(PrintStream out) throws IOException {  int[] primes = new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };    // Print the primes out using a for/in loop  for (int n : primes) {    out.println(n);  }}      

没有任何需要特别说明的地方,这些都是非常基本的东西。数组被类型化,所以您需要很清楚地知道数组中每个项目的变量类型是什么。这个示例创建了变量(在这个示例中名为 n),然后对这个变量进行操作。非常简单,不是吗?我告诉过您在这里没有什么复杂的东西。

实际上,数据中有什么类型并不是问题,您只需为 声明 选择好正确的类型就可以了。在清单 8 中,数组的元素是 Lists。所以您得到的实际上是一个集合数组。同样,使用 for/in 就能使这些变得非常简单。


清单 8. 用 for/in 还可以在对象数组上循环
        public void testObjectArrayLooping(PrintStream out) throws IOException {  List[] list_array = new List[3];    list_array[0] = getList();  list_array[1] = getList();  list_array[2] = getList();    for (List l : list_array) {    out.println(l.getClass().getName());  }}      

甚至还可以在 for/in 循环中再加上一层循环,如清单 9 所示:


清单 9. 在 for/in 内部使用 for/in 不会有任何问题!
        public void testObjectArrayLooping(PrintStream out) throws IOException {  List[] list_array = new List[3];    list_array[0] = getList();  list_array[1] = getList();  list_array[2] = getList();    for (List l : list_array) {            for (Object o : l) {      out.println(o);    }  }}      

处理集合

同样,简单性也是我们关注的内容。使用 for/in 对集合进行遍历没有任何需要特殊处理或者复杂的地方,它工作起来,与您刚才看到的处理列表和集合的方式一样。清单 10 演示了一个在 ListSet 上遍历的示例,毫无惊人之处。与往常一样,我们将研究代码,确保您了解发生的事情。


清单 10. 以下程序中有许多简单循环,演示了如何使用 for/in
        package com.oreilly.tiger.ch07;import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Set;public class ForInDemo {  public static void main(String[] args) {      // These are collections to iterate over below    List wordlist = new ArrayList();    Set wordset = new HashSet();        // Basic loop, iterating over the elements of an array    //   The body of the loop is executed once for each element of args[].    //   Each time through, one element is assigned to the variable word.    System.out.println("Assigning arguments to lists...");    for (String word : args) {      System.out.print(word + " ");      wordlist.add(word);      wordset.add(word);    }        System.out.println();        // Iterate through the elements of the List now    //   Since lists have an order, these words should appear as above    System.out.println("Printing words from wordlist " +      "(ordered, with duplicates)...");    for (Object word : wordlist) {      System.out.print((String)word + " ");    }        System.out.println();        // Do the same for the Set. The loop looks the same but by virtue    //   of using a Set, word order is lost, and duplicates are discarded.    System.out.println("Printing words from wordset " +      "(unordered, no duplicates)...");    for (Object word : wordset) {      System.out.print((String)word + " ");    }  }} 清单 14:可以利用泛型重写清单 10
        package com.oreilly.tiger.ch07;import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Set;public class ForInDemo {  public static void main(String[] args) {      // These are collections to iterate over below            List<String> wordlist = new ArrayList<String>();    Set<String> wordset = new HashSet<String>();        // Basic loop, iterating over the elements of an array    //   The body of the loop is executed once for each element of args[].    //   Each time through, one element is assigned to the variable word.    System.out.println("Assigning arguments to lists...");    for (String word : args) {      System.out.print(word + " ");      wordlist.add(word);      wordset.add(word);    }        System.out.println();        // Iterate through the elements of the List now    //   Since lists have an order, these words should appear as above    System.out.println("Printing words from wordlist " +      "(ordered, with duplicates)...");    for (        String word : wordlist) {      System.out.print((String)word + " ");    }        System.out.println();        // Do the same for the Set. The loop looks the same but by virtue    //   of using a Set, word order is lost, and duplicates are discarded.    System.out.println("Printing words from wordset " +      "(unordered, no duplicates)...");    for (        String word : wordset) {      System.out.print((String)word + " ");    }  }}

不能做什么

我确实认为 for/in 是这些好东西中的一个,但是与所有的好东西一样,它们也有自身的局限性。原因是 for/in 设置的方式,特别是因为它没有显式地使用 Iterator,所以使用这个新构造时,有些事情是您不能做的。

定位

最明显的显然是不能确定您在列表或数组(或者定制对象)中的位置。为了提醒您,清单20 显示了典型 for 循环的一个可能用法。请注意,索引变量不仅能是在列表中移动,还能指示其所在位置:


清单 20. 在普通的循环中使用迭代变量
        List<String> wordList = new LinkedList<String>();for (int i=0; i<args.length; i++) {  wordList.add("word " + (i+1) + ": '" + args[i] + "'");}      

这不是什么古怪的用法,而是很普通的编程方式。但是,您不能用 for/in 完成这个简单的任务,如清单 21 所示:


清单 21. 不可能在 for/in 循环中访问位置
        public void determineListPosition(PrintStream out, String[] args)    throws IOException {        List<String> wordList = new LinkedList<String>();                // Here, it's easy to find position    for (int i=0; i<args.length; i++) {      wordList.add("word " + (i+1) + ": '" + args[i] + "'");    }                // Here, it's           not possible to locate position            for (String word : wordList) {      out.println(word);    }}      

在这里,没有任何类型的计数器变量(或者 Iterator),也不存在任何侥幸。如果需要定位,就得用“普通”的 for。清单 22 显示了定位的另外一个常见用法 —— 处理字符串:


清单 22. 另一个问题 —— 字符串连接
        StringBuffer longList = new StringBuffer();for (int i=0, len=wordList.size(); i < len; i++) {  if (i < (len-1)) {    longList.append(wordList.get(i))            .append(", ");  } else {    longList.append(wordList.get(i));  }}out.println(longList);      

删除项目

另外一个限制是项目删除。如清单 23 所示,在列表遍历期间无法删除项目:


清单 23. 在 for/in 循环中无法删除项目
        public void removeListItems(PrintStream out, String[] args)    throws IOException {        List<String> wordList = new LinkedList<String>();        // Assign some words    for (int i=0; i<args.length; i++) {      wordList.add("word " + (i+1) + ": " '" + args[i] + "'");    }        // Remove all words with "1" in them.         Impossible with for/in!    for (Iterator i = wordList.iterator(); i.hasNext(); ) {      String word = (String)i.next();      if (word.indexOf("1") != -1) {                i.remove();      }    }        // You can print the words using for/in    for (String word : wordList) {      out.println(word);    }}      

从整体来看,这些不算什么限制,只是什么时候使用 for、什么时候使用 for/in 的一个准则。可能是一些不值一提的细节。

最糟糕的结果是您可能找不到 需要 for/in 的地方,这也正是我所担心的。请记住,for/in 是一项很方便的功能,它能让代码更清晰、更简洁,同时也能让代码简洁得让人头痛。

 
http://www.ibm.com/developerworks/cn/java/j-forin.html