合并两个已按升序排序的数组

时间:2014-12-07 18:16:27

标签: java arrays merge

我需要在一个数组中合并2个数组这两个数组已经按递增顺序排序,我应该将它们合并到另一个数组中我们首先应该比较数组1和数组2并将较小的数组放在数组3中并仅增加数组3如果一个数组完全合并,我们应该复制第二个数组的其余部分。 这是我的代码,直到知道我没有想法为什么它给我带来了界限

       import java.util.Scanner;


       public class Problem5 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner scan=new Scanner(System.in);
    System.out.println("Enter the size of the two arrays: ");
    int s1=scan.nextInt();
    int s2=scan.nextInt();
    System.out.println("Enter "+s1+" numbers sorted in the increasing order:");
    int[] array1=new int[s1];
    for(int i=0;i<array1.length;i++)
        array1[i]=scan.nextInt();
    System.out.println("Enter "+s2+" numbers sorted in the increasing order:");
    int[] array2=new int[s2];
    for(int i=0;i<array2.length;i++)
        array2[i]=scan.nextInt();
    int[] array3=new int[s1+s2];
    System.out.print("The result of merging the two arrays is : ");
    mergeArrays(array1,array2,array3);
    for(int i=0;i<array3.length;i++)
      System.out.print (array3[i]+" ");
    }
public static void mergeArrays(int[] array1,int[] array2,int[] array3){
    int one=0;
    int two=0;
    for(int k=0;k<array3.length;k++){
        if(one<array1.length && array1[one]<array2[two]){
            array3[k]=array1[one++];
            System.out.println(array3[k]);}
        else if( two<array2.length && array2[two]<array1[one] ){
            array3[k]=array2[two++];
            System.out.println(array3[k]);}}

}

  }

输出仍然是错误的

                 Enter the size of the two arrays: 
                 3
                 5
                Enter 3 numbers sorted in the increasing order:
                 2
                 8
                 14
               Enter 5 numbers sorted in the increasing order:
               1
               9
               12
               17
               20
              Exception in thread "main" The result of merging the two arrays is : 1
                                                                                   2 
                                                                                   8
                                                                                   9
                                                                                   12
                                                                                   14
                                       java.lang.ArrayIndexOutOfBoundsException: 3
                                    at Problem5.mergeArrays(Problem5.java:33)
                                    at Problem5.main(Problem5.java:22)

1 个答案:

答案 0 :(得分:1)

public static void mergeArrays(int[] array1, int[] array2, int[] array3) {
    int one = 0;
    int two = 0;
    for (int k = 0; k < array3.length; k++) {
        if (one < array1.length && array1[one] < array2[two]) {  // <== (D) On the next step you try to get array2[two] which is out of bounds.
            array3[k] = array1[one++]; // <== (A) On last array1 element you make one == array1.length
            System.out.println(array3[k]);
        }
        else if (two < array2.length && array2[two] < array1[one]) { // <== (B) On the next step you try to get array1[one] which is out of bounds.
            array3[k] = array2[two++]; // <== (C) On last array2 element you make two == array2.length
            System.out.println(array3[k]);
        }
    }

}

E.g。你有array1 = {1},array2 = {2},array3 = {?,? }。

  • (A)k = 0,1 = 0,2 = 0 =&gt;第一个条件== true =&gt;一个递增
  • (B)k = 1,1 = 1,2 = 0 =&gt;第一个条件== false,第二个条件试图评估array1 [one],这会导致异常。

E.g。你有array1 = {2},array2 = {1},array3 = {?,? }。

  • (C)&amp; (D)以镜像方式工作,试试自己。

正确的方式:

public static void mergeArrays(int[] array1, int[] array2, int[] array3) {
    int one = 0;
    int two = 0;
    for (int k = 0; k < array3.length; k++) {
        if (two >= array2.length || one < array1.length && array1[one] < array2[two]) {
            array3[k] = array1[one++];
            System.out.println(array3[k]);
        }
        else if (one >= array1.length || two < array2.length && array1[one] >= array2[two]) {
            array3[k] = array2[two++];
            System.out.println(array3[k]);
        }
    }

}