首页 > 代码库 > for-each 格式

for-each 格式

public class D21LX {

    public static void main(String arge[]){
       
       fish [] a1=new fish[3]; 
        a1[0] = new fish();
        a1[1] = new fish();
        a1[2] = new fish();
        
           a1[0].name="鱼";
           a1[0].type="娃娃鱼";
           a1[0].age=0;
        
           a1[1].name="鱼1";
           a1[1].type="娃娃鱼1";
           a1[1].age=1;
        
           a1[2].name="鱼2";
           a1[2].type="娃娃鱼2";
           a1[2].age=2;
        
           for(fish y:a1){
           System.out.println(y.type);
    
        }
      }
    }
    class fish{
       String name;
       String type;
       int age=10;
       
     
      }

结果:

娃娃鱼
娃娃鱼1
娃娃鱼2

for-each 格式