为什么我的Java方法适用于不存在的int [0]?

时间:2017-10-24 18:16:19

标签: java arrays recursion

这是我合并两个排序数组的方法:

public static int[] AddString(int[] a, int[] b, int pos, int n){

    if(n > 1){
        if(a[0] > b[0] && !(a.length == 0)){
            c[pos] = b[0];
            b = Arrays.copyOfRange(b, 1, b.length);
        }else{
            c[pos] = a[0];
            a = Arrays.copyOfRange(a, 1, a.length);
        }
        AddString(a, b, pos + 1, n - 1);
    }
    return c;
}

我需要组合两个排序的数组。这些是数组:

static int[] c = new int[String1.length + String2.length];
static int[] array1 = new int[]{2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
static int[] array2 = new int[]{2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};

用于调用AddString的行:

System.out.println(Arrays.toString(AddString(String1, String2, 0, n)));

现在我的应用程序返回此错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at com.company.Main.AddString(Main.java:21)
at com.company.Main.AddString(Main.java:28)
at com.company.Main.AddString(Main.java:28)
at com.company.Main.AddString(Main.java:28)
at com.company.Main.AddString(Main.java:28)
at com.company.Main.AddString(Main.java:28)
at com.company.Main.AddString(Main.java:28)
at com.company.Main.AddString(Main.java:28)
at com.company.Main.AddString(Main.java:28)
at com.company.Main.AddString(Main.java:28)
at com.company.Main.AddString(Main.java:28)
at com.company.Main.AddString(Main.java:28)
at com.company.Main.AddString(Main.java:28)
at com.company.Main.AddString(Main.java:28)
at com.company.Main.AddString(Main.java:28)
at com.company.Main.main(Main.java:14)

我理解这是因为我的应用程序正在使用不存在的数组索引,但我不明白这种情况发生在哪里。当array1为空时发生此问题。有谁可以帮助我?

1 个答案:

答案 0 :(得分:3)

a[0] > b[0] && !(a.length == 0)导致越界异常

调查一下,在检查任何长度值之前,您首先访问a[0],然后b[0]

相反,您可能需要a.length > 0 && b.length > 0 && a[0] > b[0]

请注意,java支持短路:Java logical operator short-circuiting

在破译到你想要做的事情后,你已经接近完成了它

以下是合并两个排序数组的几种不同方法

1

你的实现有点固定,递归

public static int[] merge(int[] a, int[] b, int pos, int n)
{
    if(n > 1) //expecting a or b to have an element
        if(a.length > 0)
            if(b.length > 0)
                if(a[0] > b[0]) //a and b have elements
                {
                    c[pos] = b[0];
                    b = Arrays.copyOfRange(b, 1, b.length);
                }
                else
                {
                    c[pos] = a[0];
                    a = Arrays.copyOfRange(a, 1, a.length);
                }
            else //b has no elements, a does
            {
                c[pos] = a[0];
                a = Arrays.copyOfRange(a, 1, a.length);
            }
        else //a has no elements
            if(b.length > 0)
            {
                c[pos] = b[0];
                b = Arrays.copyOfRange(b, 1, b.length);
            }

        merge(a, b, pos + 1, n - 1);
    }

    return c;
}

2

也是递归的

int[] merge(int[] a, int[] b)
{
    return merge(a, 0, b, 0, new int[a.length + b.length], 0);
}

int[] merge(int[] a, int aCounter, int[] b, int bCounter, int[] out, int outCounter)
{
    if(aCounter < a.length && bCounter < b.length) //both arrays have elements
        if(a[aCounter] < b[bCounter])
        {
            out[outCounter] = a[aCounter];
            return merge(a, aCounter + 1, b, bCounter, out, outCounter + 1);
        }
        else
        {
            out[outCounter] = b[bCounter];
            return merge(a, aCounter, b, bCounter + 1, out, outCounter + 1);
        }

    if(aCounter < a.length) //only a has elements
    {
        out[outCounter] = a[aCounter];
        return merge(a, aCounter + 1, b, bCounter, out, outCounter + 1);
    }

    if(bCounter < b.length) //only b has elements
    {
        out[outCounter] = b[bCounter];
        return merge(a, aCounter, b, bCounter + 1, out, outCounter + 1);
    }

    //no more elements
    return out;
}

3

非递归

int[] merge(int[] a, int[] b)
{
    int[] out = new int[a.length + b.length]; //out array large enough for both contents
    int ac = 0; //a index counter
    int bc = 0; //b index counter
    int outc = 0; //out index counter

    //merge until one array is fully used up
    while(ac < a.length && bc < b.length)
        out[outc++] = a[ac] < b[bc] ? a[ac++] : b[bc++];

    //add the remaining elements to out
    while(ac < a.length)
        out[outc++] = a[ac++];
    while(bc < b.length)
        out[outc++] = b[bc++];

    //return the merged array
    return out;
}