我的输出只是给出奇怪的字符

时间:2013-10-09 23:04:02

标签: java arrays methods

public static void main(String args[])
    {
        int[] a = {0, 1, 2, 3};
        int[] b = {100, 101, 102, 103, 104, 105};
        System.out.print(shuffleArrays(a, b));
    }

    public static int[] shuffleArrays(int[] a, int[] b)
    {
        int[] c = new int[a.length+b.length];
        for (int i=0; i<b.length-1; i++)
        {
            if (i<a.length-1 && i<b.length-1)
                c[i] = c[i] + (a[i] + b[i]);
            else if (i>=a.length-1)
                c[i] = c[i] + b[i];
            else if (i>=b.length-1)
                c[i] = c[i] + a[i];
        }

        return c;
    }

这给了我一个输出“[I @ 1837b90c”。不知道为什么会这样。我是否错误地调用了该方法?

2 个答案:

答案 0 :(得分:3)

您正在打印shuffleArrays返回的数组的引用。这不会自动打印出数组的内容,而是打印数组引用。您可能希望执行以下操作:

System.out.print(Arrays.toString(shuffleArrays(a, b)))

答案 1 :(得分:0)

你也有错误的方法,这个方法工作正常:

public static void main(String args[]) {
    int[] a = {0, 1, 2, 3};
    int[] b = {100, 101, 102, 103, 104, 105};
    //c[] = {0, 100, 1, 101, 2, 102, 3, 103, 104, 105}
    System.out.print(Arrays.toString(shuffleArrays(a, b)));
}

public static int[] shuffleArrays(int[] a, int[] b) {
       public static int[] shuffleArrays(int[] a, int[] b) {
    int[] c = new int[a.length + b.length];
    int[] smaller;
    int[] bigger;
    if (a.length < b.length){
        smaller = a;
        bigger = b;
    } else {
        smaller = b;
        bigger = a;
    }

    for (int i = 0; i < smaller.length; i++) {
        c[i*2] = smaller[i];
        c[i*2+1] = bigger[i];
    }
    for (int i = 0; i < bigger.length - smaller.length; i++) {
        c[smaller.length*2+i] = bigger[smaller.length+i];
    }

    return c;
}