Question 2
Given: 
10. public class Bar { 
11.static void foo(int...x) {         //不明白地方一:这儿的三个点是什么意思????
12. // insert code here 
13. } 
14. } 
Which two code fragments, inserted independently at line 12, will allow 
the class to compile? (Choose two.) 
A. foreach(x) System.out.println(z); //不明白地方二:有foreach()这样的函数????
B. for(int z : x) System.out.println(z); //for(int z : x)是什么意思??
C. while( x.hasNext()) System.out.println( x.next()); 
D. for( int i=0; i< x.length; i++ ) System.out.println(x[i]); 
Answer: BD 
谢谢

解决方案 »

  1.   

    Given:  
    10.   public   class   Bar   {  
    11.static   void   foo(int...x)   {                   //应该是笔误还是啥,反正就是定义了一个int型参数x,还可能是int[] x
    12.   //   insert   code   here  
    13.   }  
    14.   }  
    Which   two   code   fragments,   inserted   independently   at   line   12,   will   allow  
    the   class   to   compile?   (Choose   two.)  
    A.   foreach(x)   System.out.println(z);   //其他语言中有这个函数,不过java中没有
    B.   for(int   z   :   x)   System.out.println(z);   //jdk中新加的相当于常见的for(int z =0(or 1);z<x.length;z++)
    C.   while(   x.hasNext())   System.out.println(   x.next());  
    D.   for(   int   i=0;   i <   x.length;   i++   )   System.out.println(x[i]);  
    Answer:   BD 
      

  2.   


     这里考察的是J2SE 5.0的新特性
      
      public   class   Bar   {   
    static   void   foo(int...x)   {                   
      //   insert   code   here 
    问题1:                       
     //表示可变长参数 在J2SE 5.0之前 当传入到方法的参数个数不固定时,经常采用数组的方式传递参数,在之后 就采用可变长参数的特性给方法传递参数 它通过 新的for语句读取其中的值。它用三个...表示   比如我们常用的 public static void main(String []args)就可以写成
     public static void main(String ...)   }   
       }   Which two code fragments, inserted independently at line 12, will allow   
    the class to compile?(Choose   two.)   A.   foreach(x)   System.out.println(z);  
     //不明白地方二:有foreach()这样的函数???? 
    C#上面有foreach循环,在Java上面没有这个关键字
    ,但是for新循环方式却拥有这样的特性 ,所以肯定编译错误啊B.   for(int   z   :   x)   System.out.println(z);   
     //for(int   z   :   x)是什么意思??  
     for(int z:x)这是新的FOR语句形式。 比如一个数组  
     一般你都是用for(int i=0;i<array.length;i++)
     新的for 就可以是  for(int i:array) 
     主要用在遍历数组和集合。
    C.   while(   x.hasNext())   System.out.println(   x.next());
     //必须实现了iterable接口才能返回一个Iterator 然后通过hasNext() 和next()遍历
    D.   for(   int   i=0;   i <   x.length;   i++   )   System.out.println(x[i]); 
    BD是等价的  
    Answer:   BD   
      

  3.   

    static  void  foo(int...x)
    支持变长参数。
      

  4.   

    表示个数可变的参数,但是用作数组.
    例如定义:
    static void x(int... aaa) {
       for (int i : aaa) {
          System.out.println(i);
       }
    }
    这里调用aaa就是数组.
    调用x的时候,就可以说:
    x(1)  x(1, 2)  x(3, 2, 1)   都可以.这就是参数个数可变的意思. 
      

  5.   

    一:变长参数(JDK5.0后),参数的个数可以改变。
    for(int   z   :   x)是什么意思?? JDK5.0后,对for循环的改进。