Chinaunix首页 | 论坛 | 博客
  • 博客访问: 751761
  • 博文数量: 130
  • 博客积分: 2951
  • 博客等级: 少校
  • 技术积分: 1875
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-04 18:32
文章分类

全部博文(130)

文章存档

2013年(1)

2012年(129)

分类: 系统运维

2012-04-12 14:14:16

java中的增强for循环可以这样理解:

  1. for (Iterator<
  2.         E> #i = (
  3.         expression).iterator(); #i.hasNext(); ) {
  4.  
  5.         declaration = #i.next();
  6.  
  7.         statement
  8. }

例子1
  1. import java.util.*;
  2. public class TenIntegers implements Iterable<Integer> {
  3.  public Iterator<Integer> iterator() {
  4.   return new Iterator<Integer>() {
  5.    private int count = 0;
  6.    public boolean hasNext() {
  7.     return (count < 10);
  8.    }
  9.    public Integer next() {
  10.     return Integer.valueOf(count++);
  11.    }
  12.    public void remove() {
  13.     throw new UnsupportedOperationException();
  14.    }
  15.   };
  16.  }
  17.  public static void main(String[] args)
  18.  {
  19.   TenIntegers integers = new TenIntegers();
  20.   for (int i : integers)
  21.   {
  22.    System.out.println(i);/* 依次输出从“0"到“9”的十个整数 */
  23.   }
  24.  }
  25. }
例子2:

  1. package com.oreilly.tiger.ch07;
  2. import java.util.Iterator;
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. /**
  7.  * This class allows line-by-line iteration through a text file.
  8.  * The iterator's remove() method throws UnsupportedOperatorException.
  9.  * The iterator wraps and rethrows IOExceptions as IllegalArgumentExceptions.
  10.  */
  11. public class TextFile implements Iterable<String> {
  12.   // Used by the TextFileIterator below
  13.   final String filename;
  14.   
  15.   public TextFile(String filename) {
  16.     this.filename = filename;
  17.   }
  18.   
  19.   // This is the one method of the Iterable interface
  20.   public Iterator<String> iterator() {
  21.     return new TextFileIterator();
  22.   }
  23.   
  24.   // This non-static member class is the iterator implementation
  25.   class TextFileIterator implements Iterator<String> {
  26.   
  27.     // The stream being read from
  28.     BufferedReader in;
  29.     
  30.     // Return value of next call to next()
  31.     String nextline;
  32.     
  33.     public TextFileIterator() {
  34.       // Open the file and read and remember the first line
  35.       // Peek ahead like this for the benefit of hasNext()
  36.       try {
  37.         in = new BufferedReader(new FileReader(filename));
  38.         nextline = in.readLine();
  39.       } catch (IOException e) {
  40.         throw new IllegalArgumentException(e);
  41.       }
  42.     }
  43.     
  44.     // If the next line is non-null, then we have a next line
  45.     public boolean hasNext() {
  46.       return nextline != null;
  47.     }
  48.     
  49.     // Return the next line, but first read the line that follows it
  50.     public String next() {
  51.       try {
  52.         String result = nextline;
  53.         
  54.         // If we haven't reached EOF yet...
  55.         if (nextline != null) {
  56.           nextline = in.readLine(); // Read another line
  57.           if (nextline == null)
  58.             in.close(); // And close on EOF
  59.         }
  60.         
  61.         // Return the line we read last time through
  62.         return result;
  63.         
  64.       } catch (IOException e) {
  65.         throw new IllegalArgumentException(e);
  66.       }
  67.     }
  68.     
  69.     // The file is read-only; we don't allow lines to be removed
  70.     public void remove() {
  71.       throw new UnsupportedOperationException();
  72.     }
  73.   }
  74.   
  75.   public static void main(String[] args) {
  76.     String filename = "TextFile.java";
  77.     if (args.length > 0)
  78.       filename = args[0];
  79.       
  80.     for (String line : new TextFile(filename))
  81.       System.out.println(line);
  82.   }
  83. }




阅读(4241) | 评论(0) | 转发(0) |
0

上一篇:JAVA 泛型 - 补充拾遗

下一篇:JAVA - Annotation

给主人留下些什么吧!~~