紫風亂寫

          除了他眼前的屏幕,這個人什么也沒看見。
          被周圍的電腦簇擁著,他只知道他所創造的現實,但又意識到那是虛幻。
          他已經超越了技術。也超越了機器。
          posts - 62, comments - 93, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Java 5.0中改進的for循環

          Posted on 2005-09-12 23:44 Justfly Shi 閱讀(1650) 評論(0)  編輯  收藏 所屬分類: Study Tiger
          改進的for循環是Java 5.0中的一個讓我很喜歡的改進。它只對數組和實現了java.util.Iterable接口的容器類有效。
          package cn.justfly.study.tiger.enhancedfor;

          import java.util.ArrayList;
          import java.util.Collection;


          /**
           * The demo of enhanced for statement
           * it can only used for array and classes implements java.util.Iterable interface
           * @author Justfly Shi
           * created at 2005-8-28 21:42:12
           
          */
          public class EnhancedFor {

            
          /**
             * @param args
             
          */
            
          public static void main(String[] args) {
              
          //for array
              int[] intArray={1,2,3,4};
              System.
          out.println("printing ints:");
              
          for(int i:intArray){
                System.
          out.println(i);   
              }
              
              
          //for Collection
             Collection<String> list=new ArrayList<String>();
              
          for(int i=0;i<4;i++){
                list.add(
          "String"+i);
              }
              System.
          out.println("print Strings in an Collection:");
              
          for(String i:list){
                System.
          out.println(i);
              }
              
              
          //self-define Iterable
              MyIterable<String> myIte=new MyIterable<String>(list);
              System.
          out.println("print Stings in an Iterable");
              
          for(String i:myIte){
                System.
          out.println(i);
              }
              
            }

          }

          package cn.justfly.study.tiger.enhancedfor;

          import java.util.Collection;
          import java.util.Iterator;

          /**
           * an self-defined Iterable ,that can be used in enhanced-for statement 
           * @author Justfly Shi
           * created at 2005-8-28 22:09:05
           * @param 
           
          */
          public class MyIterable<G_E> implements Iterable<G_E> {
            
          private Collection<G_E> _list;

            
          public Iterator<G_E> iterator() {
              
          return new MyIterator<G_E>(_list.iterator());
            }

            
          public MyIterable(Collection<G_E> list) {
              _list 
          = list;
            }

            
          class MyIterator<G_I> implements Iterator<G_I> {
              
          private Iterator<G_I> _ite;

              MyIterator(Iterator
          <G_I> ite) {
                _ite 
          = ite;
              }

              
          public boolean hasNext() {
                
          return _ite.hasNext();
              }

              
          public G_I next() {
                
          return _ite.next();
              }

              
          public void remove() {
                _ite.remove();

              }
            }
          }

          輸出結果
          printing ints:
          1
          2
          3
          4
          print Strings in an Collection:
          String0
          String1
          String2
          String3
          print Stings in an Iterable
          String0
          String1
          String2
          String3
          主站蜘蛛池模板: 久治县| 元江| 右玉县| 高台县| 阿勒泰市| 泽州县| 碌曲县| 西林县| 偃师市| 扬中市| 徐水县| 疏勒县| 德保县| 农安县| 钦州市| 黑龙江省| 陆川县| 滕州市| 商洛市| 营口市| 保山市| 盘锦市| 高州市| 黔东| 沈阳市| 台前县| 连州市| 宣威市| 罗城| 台中县| 壶关县| 天气| 山西省| 建湖县| 镇江市| 朝阳市| 福海县| 茶陵县| 南乐县| 游戏| 三台县|