• 用 for/in 在 Java 5.0 中增强循环


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

    简介: for/in 循环通常叫作 增强的 for 或者 foreach,它是 Java 5.0 中一个极为方便的特性。实际上它没有提供任何新的功能,但它显然能让一些日常编码任务变得更简单一些。在本文中,您将学习这方面的许多内容,其中包括使用 for/in 在数组和集合中进行遍历,以及如何用它避免不必要(或者只是令人厌烦的)类型转换。您还将学习如何实现 for/in,了解新的 Iterable 接口的一些细节,甚至还将学习如何让您自己的定制对象用这个新的构造进行遍历。最后,您将学习 for/in 不能 做什么,以确保您能理解什么时候选择原来的 for 是正确的选择。


    越短越好
    这是资深电脑程序员都知道的一条最基本的原理:因为 更短 意味着 打字更少,所以更短自然也就更好。这个哲学造就了 vi 这样的 IDE,在这类 IDE 中,像 :wq! 和 28G 这样的命令拥有丰富的含义。这个哲学还导致一些最神秘的代码,比如说,变量 ar 代表 Agile Runner(也可能是 Argyle,或者 Atomic Reactor 等等,总之,您明白就好)。
    有些时候,在努力实现短小的时候,程序员会将明确性抛到脑后。也就是说,过于短小和过于繁冗的代码都会让人感到痛苦不堪。变量名为 theAtomicReactorLocatedInPhiladelphia 与名为 ar 的变量一样让人讨厌和不方便。一定会有一个让人高兴的解决方法,不是吗?

    这个让人高兴的方法(至少我是这么认为的)是以寻找完成某事的 方便 途径为出发点,不是为了短小而短小。作为这类解决方案的一个好例子,Java 5.0 引入了新版的 for 循环,我把它称为 for/in。它也被称为 foreach,有时也叫作 增强的 for,但这些指的都是同一个构造。不管您叫它什么, for/in 都会使代码变得更简单,正如您在本文中将看到的那样。


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

    清单 1. for 循环,旧式学院风格

    注意:如果您一直在看我写的关于 Tiger 新特性的文章(请参阅 参考资料),您就会知道,我常常感谢 O'Reilly Media, Inc.,因为它们允许我在本文中发布我其他书中的代码示例。这意味着您得到的代码已经通过了更多测试、更多评论,比我能提供给您的多得多。所以再次感谢 O'Reilly,如果您想了解 Tiger 的更多内容,请参考我撰写的一些书,它们列在 参考资源一节中,其中有完整的链接和更多的细节。
    如果您期待着得到如何把这个代码转变成新的 for/in 循环的详细解释,我恐怕要让您失望。清单 2 显示了用 for/in 改写的清单 1 中的代码,您应该相当熟悉它。请参见下面代码清单,我将尽可能详细地解释 for/in 循环(但是仍然很难凑成一章)。

    清单 2. 转换成 for/in    
    for/in 循环的基本语法如清单 3 所示。如果您还不习惯阅读规范,那么该语法可能看起来有点古怪,但是当您一个部分一个部分了解它的时候,您会发现阅读它实际上非常容易。

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

    for/in 因何得名
    细心的读者会注意到,所谓 for/in 根据不包含单词 in。它的名字来自借阅的阅读方式。在清单 2 中,您会说 for 每个对象 in 命名变量列表中,执行 ... 。当然,省略号代表循环实质做的操作。您如何看待会有些差异,但是在每种表达方式中 for 和 in 都是突出的。
    声明 是一个变量,例如 Object listElement。这个变量应该有自己的类型,这样,它就可以与将遍历的列表、数组或集合中的每一个项兼容。在清单 2 的例子中, list 包含一些对象,因此这些对象就是 listElement 的类型。
    表达式 就是一个表达式。它计算的结果应当是可以遍历的(后面再详加介绍)。在现在,只要保证 表达式 计算的结果是一个集合或者数组就可以了。表达式可以简单到就是一个变量(如清单 2 所示)或者是一个方法调用(例如 getList()),亦或是包含布尔逻辑或三目运算符的复杂表达式。只要它返回一个数组或集合,就一切 OK。
    语句 代表循环的内容,它对 声明 中定义的变量进行操作;当然,这是一个循环,所以 语句 将应用到数组中集合的每个项目上。而且,使用大括号( { 和 })时,还能使用多条语句。
    其用法如下:创建一个变量,指向要遍历的数组或集合,然后对定义的变量进行操作。不用对列表中的每个项目进行赋值,因为 for/in 替您处理了这件事。当然,如果您还觉得不太清楚,没关系,继续读下去,有大量的示例让您足够清楚这个事件。
    但是,在进行下一步之前,我想用更加符合规范的方式说明 for/in 的工作方式。清单 4 显示了在提供通用化类型时,实际发挥作用的 for/in 循环。以下是编译器把该循环转换成普通的 for 循环之后,语句实际看起来的样子。
    您明白了吗?编译器实际上把这个更短、更方便的 for/in 语句变成了一个更加编译器友好的 for 循环,而且您不会受到这项工作的影响。这就是为什么我认为它方便,而不仅仅说它更简短的原因。


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

    for (Iterator<
            E> #i = (
            expression).iterator(); #i.hasNext(); ) {
     
            declaration = #i.next();
     
            statement
    }
          


    清单 5 是另外一个经过编译器转换之后的 for/in,这次没有通用化类型。虽然更简单,但做的事是一样的。但是在每种情况下,您都可以很容易地在脑子里(并通过编程方式)把 for/in 语句转换成普通的 for 语句,如果您能在脑子子里做这个转换,事情就变得极为容易了。


    清单 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 演示了一个在 List 和 Set 上遍历的示例,毫无惊人之处。与往常一样,我们将研究代码,确保您了解发生的事情。


    清单 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 + " ");
        }
      }
    }
          


    清单 11 显示了这个程序的输出(在命令行上输出了一些用来演示的数据):


    清单 11. 输出正是您想要的 —— 许多打印!

            
    run-ch07:
        [echo] Running Chapter 7 examples from Java 5.0 Tiger: A Developer's Notebook
        [echo] Running ForInDemo...
        [java] Assigning arguments to lists...
        [java] word1 word2 word3 word4 word1
        [java] Printing words from wordList (ordered, with duplicates)...
        [java] word1 word2 word3 word4 word1
        [java] Printing words from wordset (unordered, no duplicates)...
        [java] word4 word1 word3 word2
          


    回页首
    类型转换之痛
    迄今为止,在处理集合的时候,您已经看到 for/in 使用通用的变量类型,例如 Object。这么做很好,但是没有真正利用到 Tiger 的另一项特性 —— 泛型(有时也叫作 参数化类型)。我把泛型的细节留给 developerWorks 即将针对这个主题推出的教程,但是泛型让 for/in 变得更加强大。
    记得 for/in 语句的 声明 部分创建了一个变量,它代表要遍历的集合中每个项目的类型。在数组中,类型非常明确,因为类型是强类型的, int[] 只能包含整数,所以在循环中没有类型转换。在您通过泛型使用类型化列表时,也有可能做到这点。清单 12 演示了几个简单的参数化集合:


    清单 12. 向集合类型添加参数意味着可以避免以后的类型转换

            
    List
            <String> wordlist = new ArrayList
            <String>();
    Set
            <String> wordset = new HashSet
            <String>();
          


    现在,您的 for/in 循环可以避开老式的 Object,变得更加具体。清单 13 演示了这一点:


    清单 13. 在知道集合中的类型时,您的循环体可以更加具有类型针对性

            

            for (String word : wordlist) {
      System.out.print(word + " ");
    }
          


    作为一个更加完整的示例,清单 14 沿用了清单 10 所示的程序,并添加了一些通用的列表和更加具体的 for/in 循环:


    清单 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 + " ");
        }
      }
    }
          


    当然,在这些例子中,类型转换还没有完全消失。但是,这些工作正逐步转交给编译器完成(如果您对这类事情感兴趣,那么可以说这就是泛型或多或少要做的事)。在编译的时候,所有这些类型都会被检测,您可能得到相应的错误信息。如果有人能做这项工作,那么,其他所有人也能这么做,不是吗?
    Who the heck is E?
    如果您是 Java 老手,但是刚接触 Tiger,那么所有对 E 的引用对您来说可能很奇怪。这些都是与参数化类型支持(泛型)有关,它允许 Iterator 可以处理类型化的集合 —— 例如, Iterator<String> 能处理这个新版本接口,敬请参阅 developerWorks 即将在 12 月 7 日推出的关于泛型的教程。
    回页首
    类与 for/in 的集成
    迄今为止,我只是针对 Java 事先打包的类和类型(array、list、map、set 和其他集合)进行遍历。尽管这已经相当不错,但编程语言的美丽在于它们能帮助您定义自己的类。定制对象是大型应用程序的支柱。这一节要处理的只是允许 for/in 构造使用您自己的对象所涉及的一些概念与步骤。
    一个新接口
    到了现在,您应当熟悉 java.util.Iterator 接口了,倘若您不熟悉它,清单 15 演示了这个接口,而且是按照它在 Tiger 出现的形式演示的:


    清单 15. Iterator 长时间以来一直是 Java 语言的中流砥柱

            
    package java.util;
    public interface Iterator<E> {
      public boolean hasNext();
      
      public E next();
      
      public void remove();
    }
          


    但是,为了利用 for/in,需要在您的域知识中添加另一个接口 java.lang.Iterable。该接口如清单 16 所示:


    清单 16. Iterable 接口是 for/in 构造的基础

            
    package java.lang;
    public interface Iterable<E> {
      public java.util.Iterator<E> iterator();
    }
          


    是 java.lang,而不是 java.util
    请注意, Iterable 位于 java.lang 之中,而 不是位于 java.util 中。至于为什么会这样,我没有找到任何明确的文档,但就我个人猜测,可能是为了避免必须导入接口( java.lang 位于为所有 Java 代码自动导入的名称空间集中)。
    为了让您的对象或类能与 for/in 一起工作,对象和类需要实现 Iterable 接口。这留给您两个基本场景:
    扩展现有的、已经实现了 Iterable(因此也就已经支持 for/in)的集合类。
    手动处理遍历,定义自己的 Iterable 实现。
    手动处理遍历
    如果有可能,我极力建议您用定制对象扩展现有的集合。事情会变得极为简单,而您可以避免所有繁琐的细节。清单 17 显示了一个这样做的类:


    清单 17. 扩展现有的集合是利用 for/in 的捷径

            
    package com.oreilly.tiger.ch07;
    import java.util.LinkedList;
    import java.util.List;
    public class GuitarManufacturerList extends LinkedList<String> {
      public GuitarManufacturerList() {
        super();
      }
      public boolean add(String manufacturer) {
        if (manufacturer.indexOf("Guitars") == -1) {
          return false;
        } else {
          super.add(manufacturer);
          return true;
        }
      }
    }  
          


    因为 LinkedList 已经可以使用 for/in,所以,不需要特殊的代码,就可以在 for/in 中使用这个新类。清单 18 演示了这点,以及做到这一点需要做的工作是多么地少:


    清单 18. Iterable 接口是 for/in 构造的基础

            
    package com.oreilly.tiger.ch07;
    import java.io.IOException;
    import java.io.PrintStream;
    public class CustomObjectTester {
      
            /** A custom object that extends List */
      private GuitarManufacturerList manufacturers;
      
      public CustomObjectTester() {
        
            this.manufacturers = new GuitarManufacturerList<String>();
      }
      
      public void testListExtension(PrintStream out) throws IOException {
        // Add some items for good measure
        manufacturers.add("Epiphone Guitars");
        manufacturers.add("Gibson Guitars");
        
        // Iterate with for/in
        
            for (String manufacturer : manufacturers) {
          out.println(manufacturer);
        }
      }
      
      public static void main(String[] args) {
        try {
          CustomObjectTester tester = new CustomObjectTester();
          
          tester.testListExtension(System.out);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
          


    手动处理遍历
    在某些不常见的情况下 —— 老实说,我费了很大劲想到了很多 —— 在您的定制对象可以遍历的时候,您可能需要执行特定的行为。在这些(相当不幸)的情况下,您必须自己处理这些事情。清单 19 演示了如何做,虽然需要做很多工作,但是并不复杂,所以我把代码留给您自己来看。以下这个类提供了文本文件的包装器,在遍历它的时候,它将列出文件中的每行内容。


    清单 19. 耐心点,您自己也能实现 Iterable 接口,并在循环中提供定制行为

            
    package com.oreilly.tiger.ch07;
    import java.util.Iterator;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    /**
     * This class allows line-by-line iteration through a text file.
     *   The iterator's remove() method throws UnsupportedOperatorException.
     *   The iterator wraps and rethrows IOExceptions as IllegalArgumentExceptions.
     */
    public class TextFile implements Iterable<String> {
      // Used by the TextFileIterator below
      final String filename;
      
      public TextFile(String filename) {
        this.filename = filename;
      }
      
      // This is the one method of the Iterable interface
      public Iterator<String> iterator() {
        return new TextFileIterator();
      }
      
      // This non-static member class is the iterator implementation
      class TextFileIterator implements Iterator<String> {
      
        // The stream being read from
        BufferedReader in;
        
        // Return value of next call to next()
        String nextline;
        
        public TextFileIterator() {
          // Open the file and read and remember the first line
          //   Peek ahead like this for the benefit of hasNext()
          try {
            in = new BufferedReader(new FileReader(filename));
            nextline = in.readLine();
          } catch (IOException e) {
            throw new IllegalArgumentException(e);
          }
        }
        
        // If the next line is non-null, then we have a next line
        public boolean hasNext() {
          return nextline != null;
        }
        
        // Return the next line, but first read the line that follows it
        public String next() {
          try {
            String result = nextline;
            
            // If we haven't reached EOF yet...
            if (nextline != null) {
              nextline = in.readLine();    // Read another line
              if (nextline == null)
                in.close();                // And close on EOF
            }
            
            // Return the line we read last time through
            return result;
            
          } catch (IOException e) {
            throw new IllegalArgumentException(e);
          }
        }
        
        // The file is read-only; we don't allow lines to be removed
        public void remove() {
          throw new UnsupportedOperationException();
        }
      }
      
      public static void main(String[] args) {
        String filename = "TextFile.java";
        if (args.length > 0)
          filename = args[0];
          
        for (String line : new TextFile(filename))
          System.out.println(line);
      }
    }
          


    其中大部分工作是实现 Iterator,然后通过 iterator() 方法返回它。其他的事情就非常简单了。但是,您可以看到,与扩展一个现成的类来完成同样的工作相比,手动实现 Iterable 接口需要做的工作多得多。
    回页首
    不能做什么
    我确实认为 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 是一项很方便的功能,它能让代码更清晰、更简洁,同时也能让代码简洁得让人头痛。
  • 相关阅读:
    spring mvc给参数起别名
    聊聊分布式定时任务中间件架构及其实现--转
    Batch Normalization的算法本质是在网络每一层的输入前增加一层BN层(也即归一化层),对数据进行归一化处理,然后再进入网络下一层,但是BN并不是简单的对数据进行求归一化,而是引入了两个参数λ和β去进行数据重构
    终端安全工具 gartner 排名
    When Cyber Security Meets Machine Learning 机器学习 安全分析 对于安全领域的总结很有用 看未来演进方向
    DNS隧道之DNS2TCP实现——dns2tcpc必须带server IP才可以,此外ssh可以穿过墙的,设置代理上网
    DNS隧道之DNS2TCP使用心得教程——是可以用来穿透qiang的,ubuntu下直接apt install dns2tcp
    DNS隧道工具汇总——补充,还有IP over DNS的工具NSTX、Iodine、DNSCat
    Data Mining and Machine Learning in Cybersecurity PDF
    ES failed to notify ClusterStateListener java.lang.IllegalStateException: environment is not locked
  • 原文地址:https://www.cnblogs.com/eustoma/p/2415779.html
Copyright © 2020-2023  润新知