2014-03-06 109 views
1
/* 
* Given an array of positive ints, return a new array of length "count" containing the 
* first even numbers 
* from the original array. The original array will contain at least "count" even numbers. 
*/ 

public class StringEx 
{ 

    public static void main(String[] args) 
    { 
     int[] nums = {2,3,5,6,8}; 
     int count = 2; 
     StringEx s1 = new StringEx(); 
     System.out.println(s1.copyEvens(nums, count)); 

    } 
    public int[] copyEvens(int[] nums, int count) 
    { 
     int[] n=new int[count]; 
     int c=0; 

     for(int i=0;i<nums.length;i++) 
     { 
      if(nums[i]%2==0&&c!=count) 
      { 
       n[c]=nums[i]; 
       c++; 

      } 
     } 

     return n; 
    } 
} 

// Output:[[email protected] 

回答

6

阵列没有一个不错的toString方法。他们使用Object.toString,这给出

getClass().getName() + '@' + Integer.toHexString(hashCode()) 

这通常是无益的。如果您想要可读表示,请使用Arrays.toString

+0

我如何知道结果?这个System.out.println(s1.copyEvens(Arrays.toString(nums),count));肯定是不可接受的! – Nandan

+1

@南丹:你错了。转换'copyEvens'返回值,而不是输入。 – user2357112