ArrayIndexoutofbound异常合并两个已排序的数组

时间:2015-09-18 04:56:41

标签: java

我见过其他解决方案,例如this,并想知道我使用for循环的方法是否有问题。我不想像其他人在他们的解决方案中那样使用while循环。

package com.my.practice;

public class MedianOfTwoArrays {

    public static void main(String[] args) {
        // Given two sorted arrays of same size

        int[] a1 = {1,2,3,4,5,6};
        int[] a2 = {7,8,9,10,11,12};        

        int[] mergedArray = new int[a1.length + a2.length];

        for(int i=0 ; i < a1.length; i++){
            mergedArray[i] = a1[i];
        }

        //System.out.println("Length:"+2*(a1.length));

        for(int i= a1.length; i < 2 * (a1.length); i++) {
            mergedArray[i] = a2[i];
        }

        for(int i=0 ; i < 2*(a1.length); i++){          
            System.out.println("Part of Array: "+mergedArray[i]+ " Length is: "+mergedArray.length);
        }
    }
}

我收到以下错误:

Length:12
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at com.my.practice.MedianOfTwoArrays.main(MedianOfTwoArrays.java:30)

5 个答案:

答案 0 :(得分:1)

依靠两个长度相同的数组并不是一个好主意,所以我不会使用a1.length * 2.此外,你不能使用相同的索引对于原始数组和合并数组,因为合并的数组更长。

建议修正:

int[] mergedArray = new int[a1.length + a2.length];

for(int i=0 ; i < a1.length; i++){
    mergedArray[i] = a1[i];
}

for(int i= 0 ; i < a2.length; i++){
    mergedArray[a1.length + i] = a2[i];
}

for(int i=0 ; i < mergedArray.length; i++){
    System.out.println("Part of Array: "+mergedArray[i]+ " Length is: "+mergedArray.length);
}

答案 1 :(得分:1)

您的代码中的问题是由于在第二个循环中使用-r <arg>, --revision <arg>作为i的索引。它适用于a2但不适用于mergedArray

在第二个循环中使用a2作为索引

i-a1.length

或使用三个指数:for(int i= a1.length; i < 2 * (a1.length); i++) { mergedArray[i] = a2[i-a1.length]; } i&amp; j。只是为了给你一个想法

k

答案 2 :(得分:1)

在这一行:

[ERROR] :  DOWNLOAD PROGRESS : 67470/5794757
[ERROR] :  DOWNLOAD PROGRESS : 133006/5794757
[ERROR] :  DOWNLOAD PROGRESS : 199614/5794757
[ERROR] :  DOWNLOAD PROGRESS : 266222/5794757
[ERROR] :  DOWNLOAD PROGRESS : 275982/5794757
[ERROR] :  DOWNLOAD PROGRESS : 341518/5794757
[ERROR] :  DOWNLOAD PROGRESS : 408126/5794757
[ERROR] :  DOWNLOAD PROGRESS : 474734/5794757
[ERROR] :  DOWNLOAD PROGRESS : 541342/5794757
[ERROR] :  DOWNLOAD PROGRESS : 609398/5794757
[ERROR] :  DOWNLOAD PROGRESS : 677454/5794757
[ERROR] :  DOWNLOAD PROGRESS : 745510/5794757
[ERROR] :  DOWNLOAD PROGRESS : 758166/5794757
[ERROR] :  DOWNLOAD PROGRESS : 823702/5794757
[ERROR] :  DOWNLOAD PROGRESS : 897550/5794757
[ERROR] :  DOWNLOAD PROGRESS : 1048142/5794757
[ERROR] :  DOWNLOAD PROGRESS : 1114750/5794757
[ERROR] :  DOWNLOAD PROGRESS : 1182806/5794757
[ERROR] :  DOWNLOAD PROGRESS : 1249414/5794757
[ERROR] :  DOWNLOAD PROGRESS : 1316022/5794757
[ERROR] :  DOWNLOAD PROGRESS : 1382630/5794757
[ERROR] :  DOWNLOAD PROGRESS : 1385150/5794757
[ERROR] :  DOWNLOAD PROGRESS : 1450686/5794757
[ERROR] :  DOWNLOAD PROGRESS : 1518742/5794757
[ERROR] :  DOWNLOAD PROGRESS : 1586798/5794757
[ERROR] :  DOWNLOAD PROGRESS : 1653406/5794757
[ERROR] :  DOWNLOAD PROGRESS : 1720014/5794757
[ERROR] :  DOWNLOAD PROGRESS : 1786622/5794757
[ERROR] :  DOWNLOAD PROGRESS : 1854678/5794757
[ERROR] :  DOWNLOAD PROGRESS : 1921286/5794757
[ERROR] :  DOWNLOAD PROGRESS : 1989342/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2057398/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2059918/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2125454/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2205094/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2271702/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2341206/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2342278/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2407814/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2410334/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2475870/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2542478/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2609086/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2675694/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2676766/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2742302/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2808910/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2876966/5794757
[ERROR] :  DOWNLOAD PROGRESS : 2943574/5794757
[ERROR] :  DOWNLOAD PROGRESS : 3010182/5794757
[ERROR] :  DOWNLOAD PROGRESS : 3076790/5794757
[ERROR] :  DOWNLOAD PROGRESS : 3143398/5794757
[ERROR] :  DOWNLOAD PROGRESS : 3212902/5794757
[ERROR] :  DOWNLOAD PROGRESS : 3274790/5794757
[ERROR] :  DOWNLOAD PROGRESS : 3340326/5794757
[ERROR] :  DOWNLOAD PROGRESS : 3422862/5794757
[ERROR] :  DOWNLOAD PROGRESS : 3557526/5794757
[ERROR] :  DOWNLOAD PROGRESS : 3624134/5794757
[ERROR] :  DOWNLOAD PROGRESS : 3692190/5794757
[ERROR] :  DOWNLOAD PROGRESS : 3758798/5794757
[ERROR] :  DOWNLOAD PROGRESS : 3825406/5794757
[ERROR] :  DOWNLOAD PROGRESS : 3893462/5794757
[ERROR] :  DOWNLOAD PROGRESS : 3960070/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4026678/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4094734/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4161342/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4227950/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4294558/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4362614/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4430670/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4497278/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4498350/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4563886/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4630494/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4697102/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4699622/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4765158/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4831766/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4898374/5794757
[ERROR] :  DOWNLOAD PROGRESS : 4964982/5794757
[ERROR] :  DOWNLOAD PROGRESS : 5031590/5794757
[ERROR] :  DOWNLOAD PROGRESS : 5098198/5794757
[ERROR] :  DOWNLOAD PROGRESS : 5106510/5794757
[ERROR] :  DOWNLOAD PROGRESS : 5172046/5794757
[ERROR] :  DOWNLOAD PROGRESS : 5319742/5794757
[ERROR] :  DOWNLOAD PROGRESS : 5320814/5794757
[ERROR] :  DOWNLOAD PROGRESS : 5386350/5794757
[ERROR] :  DOWNLOAD PROGRESS : 5455854/5794757
[ERROR] :  DOWNLOAD PROGRESS : 5523910/5794757
[ERROR] :  DOWNLOAD PROGRESS : 5591966/5794757
[ERROR] :  DOWNLOAD PROGRESS : 5658574/5794757
[ERROR] :  DOWNLOAD PROGRESS : 5726630/5794757
[ERROR] :  DOWNLOAD PROGRESS : 5794686/5794757
[ERROR] :  DOWNLOAD PROGRESS : 5794757/5794757
[ERROR] :  download completed {"taskIdentifier":1,"data":{},"bubbles":true,"type":"downloadcompleted","source":{},"cancelBubble":false}
[ERROR] :  open on /var/mobile/Containers/Data/Application/9FBAAEB4-4878-4611-8BCC-EFC112AF9777/Documents/myplvideo.mp4: File exists
[ERROR] :  data : 5794757
[ERROR] :  file : file:///var/mobile/Containers/Data/Application/9FBAAEB4-4878-4611-8BCC-EFC112AF9777/Documents/myplvideo.mp4
[ERROR] :  file resolve path :/var/mobile/Containers/Data/Application/9FBAAEB4-4878-4611-8BCC-EFC112AF9777/Documents/myplvideo.mp4
[ERROR] :  sessioncompleted {"success":true,"message":"","errorCode":0,"taskIdentifier":1,"bubbles":true,"type":"sessioncompleted","source":{},"cancelBubble":false}

您尝试从{6}到for (int i= a1.length; i < 2 * (a1.length); i++) { mergedArray[i] = a2[i]; } 访问a2[i]。由于i是一个大小为6的数组,a2a2[6] .. 。a2[7]不存在。

在您的情况下,您希望将值a2[11]插入a2[1],将mergedArray[6]插入a2[2]等。

您需要在右侧减去mergedArray[7]

a1.length

或在左侧添加for (int i = a1.length; i < 2 * (a1.length); i++) { mergedArray[i] = a2[i - a1.length]; }

a1.length

选择更方便的一个。

答案 3 :(得分:1)

错误(对于ArrayOutOfBoundException):

    for(int i= a1.length; i < 2 * (a1.length); i++) {
        mergedArray[i] = a2[i];  //Using 'i' incorrectly to access a2[i]
    }

纠正上述内容:

    for(int i= 0; i < a2.length; i++) {
        mergedArray[i+a1.length] = a2[i];  //Using 'i' incorrectly to access a2[i]
    }

原因:

  • '我'这里将确保a2不超过其大小,因为我的最大限制是a2.length。
  • 不要认为a2和a1的大小相等。使用a1.length遍历a1并在遍历a2时,使用a2.length。
  • 可以添加安全(冗余)检查以确保(i + a1.length)不超过mergedArray大小。在这种情况下冗余,因为mergedArray的长度= a1.size + a2.size。

答案 4 :(得分:1)

问题是你对不同长度的数组使用相同的索引:

for(int i= a1.length; i < 2 * (a1.length); i++)
    mergedArray[i] = a2[i];

此处在i = 6初始,a2的数组边界为0-5,但a2[i]a2[6],超出范围,这会为您提供异常。

您可以使用System.arraycopy()

跳过这些循环
 System.arraycopy(a1, 0, mergedArray,0, a1.length);
 System.arraycopy(a2, 0, mergedArray,a1.length, a2.length);

如果仍然使用循环,只需使用另一个索引变量:

for(int i = 0, j = a1.length; i < (a2.length); i++, j++)
    mergedArray[j] = a2[i];