【问题标题】:Why does my code print [I@87816d when i run this code? [duplicate]为什么我的代码在运行此代码时会打印 [I@87816d? [复制]
【发布时间】:2014-04-08 16:18:28
【问题描述】:
/*
 * 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:[I@87816d

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    数组没有很好的toString 方法。他们使用Object.toString,这给出了

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

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

    【讨论】:

    • 我如何知道结果?这个 System.out.println(s1.copyEvens(Arrays.toString(nums), count));肯定是不可接受的!
    • @Nandan:你 toStringing 错了。转换 copyEvens 返回值,而不是输入。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 2023-02-26
    • 2013-08-20
    相关资源
    最近更新 更多