今天看一资料,有如下程序,编译没有问题。。enum            CoffeeSize {
        BIG( 8 ), HUGE( 10 ), OVERWHELMING( 16 );
                // 8, 10 & 16 are passed to the constructor
        CoffeeSize( int $ounces ) {
        // constructor
                this.ounces = $ounces;
        }        private int     ounces;                 // an instance variable        public int      getOunces()
        {
                return ounces;
        }
}class           Coffee1 {
        CoffeeSize      size;                   // each instance of Coffee has an enum        public static void      main( String args[] )
        {
                Coffee1 drink1 = new Coffee1();
                drink1.size = CoffeeSize.BIG;                Coffee1 drink2 = new Coffee1();
                drink2.size = CoffeeSize.OVERWHELMING;                System.out.println( drink1.size.getOunces() );
                for(CoffeeSize cs: CoffeeSize.values()) // ??!!!!!!!!!!!!!!!!!!
                        System.out.println( cs + " " + cs.getOunces() );
        }
}for(CoffeeSize cs: CoffeeSize.values()) // ??!!!!!!!!!!!!!!!!!!
                        System.out.println( cs + " " + cs.getOunces() );
这个for循环为什么是正确的- -麻烦大家了~~

解决方案 »

  1.   

    foreach语句是java1.5 的新特征之一,在遍历数组、集合方面,foreach为开发人员提供了极大的方便。
      

  2.   

    增强型For循环。。百度一搜一大堆。LZ搜搜去。
      

  3.   

      foreach  是java 5.0 之后的   优点是可以不知道要遍历的数组或者是对象数组的长度
          
      

  4.   

    jdk1.5的新特性之一:
    for(CoffeeSize cs: CoffeeSize.values()) 
      System.out.println( cs + " " + cs.getOunces() );而且,上面这个要比下面这个:
    for(int i=0; i<CoffeeSize.values().size(); i++)
      System.out.println( cs + " " + CoffeeSize.values().get(i).getOunces() );
    效率要高;
      

  5.   

    jdk1.5的新特性之一:
    for(CoffeeSize cs: CoffeeSize.values())  
      System.out.println( cs + " " + cs.getOunces() );
    楼主可以百度下
      

  6.   

    foreach循环,用来遍历数组或者实现Iterator接口的对象集
      

  7.   

    JDK新增特性 foreach用来遍历数组或者实现Iterator接口的对象集。。
      

  8.   

    明白了,谢谢楼上的各位~public void listGradeValues(PrintStream out) throws IOException {  
        for (Grade g : Grade.values()) {  
            out.println("Allowed value: '" + g + "'");  
        }