紫風(fēng)亂寫

          除了他眼前的屏幕,這個(gè)人什么也沒看見。
          被周圍的電腦簇?fù)碇恢浪鶆?chuàng)造的現(xiàn)實(shí),但又意識(shí)到那是虛幻。
          他已經(jīng)超越了技術(shù)。也超越了機(jī)器。
          posts - 62, comments - 93, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Java 5.0中改進(jìn)的for循環(huán)

          Posted on 2005-09-12 23:44 Justfly Shi 閱讀(1656) 評(píng)論(0)  編輯  收藏 所屬分類: Study Tiger
          改進(jìn)的for循環(huán)是Java 5.0中的一個(gè)讓我很喜歡的改進(jìn)。它只對(duì)數(shù)組和實(shí)現(xiàn)了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();

              }
            }
          }

          輸出結(jié)果
          printing ints:
          1
          2
          3
          4
          print Strings in an Collection:
          String0
          String1
          String2
          String3
          print Stings in an Iterable
          String0
          String1
          String2
          String3
          主站蜘蛛池模板: 四子王旗| 安西县| 民乐县| 临沂市| 德州市| 广灵县| 滦南县| 平乐县| 德令哈市| 进贤县| 香格里拉县| 西充县| 乌拉特前旗| 周口市| 南充市| 东源县| 宁明县| 黄平县| 上思县| 胶州市| 五大连池市| 丹巴县| 慈利县| 荣成市| 灵璧县| 阿克陶县| 凤山市| 邵东县| 尚义县| 祥云县| 牡丹江市| 天水市| 都江堰市| 灵石县| 临武县| 香格里拉县| 神农架林区| 徐州市| 阳东县| 凤山市| 大厂|