0

I've been trying to merge two sorted arrays into one sorted array. However, the system gives me an error even when I tried to fix it by doing counter--; Anyways, here is the full code.


import java.util.Arrays;

public class Methods2 {

    static int[] combine(int[] array1, int[] array2) {
        int[] combinedArray = new int[array1.length + array2.length];
        boolean ToF = false;
        int temp = 0;
        int counter = 0;
        for (int value : array1) {
            for (int i : array2) {
                if (value < i) {
                    temp = value;
                    counter++;
                    combinedArray = append(combinedArray, counter, temp);
                }
            }
        }
        return combinedArray;
    }

    static int[] append(int[] combinedArray, int counter, int temp) {
        counter--;
        combinedArray[counter] = temp;
        return combinedArray;
    }

    public static void main(String[] args) {
        int[] array1 = {1, 3, 5, 7, 9};
        int[] array2 = {2, 4, 6, 8, 10};
        System.out.println(Arrays.toString(combine(array1, array2)));
    }
}

Any help would be appreciated.

dizmac
  • 99
  • 9
  • 1
    What error? On what line? Did you debug the program to see what is going on "under the hood" and why the given index is out of bounds? – GBlodgett Mar 05 '20 at 00:47
  • Ok. ```Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 at Methods2.append(Methods2.java:24) at Methods2.combine(Methods2.java:15) at Methods2.main(Methods2.java:31) ``` – dizmac Mar 05 '20 at 00:48
  • And yes, I did. – dizmac Mar 05 '20 at 00:49
  • No, I tried to do counter = counter - 1. It didn't work. I am aware that <= should never be applied to array loops because it will cause an issue is array.length - 1 isn't added. – dizmac Mar 05 '20 at 00:52
  • What did you find when you debugged your code? Why is it going out of bounds? – GBlodgett Mar 05 '20 at 00:55
  • Sysout should be friend here, if not an IDE debugger. You aren't checking for bounds on both the arrays. Also, what happens when the two arrays are not of the same size? You should account for that condition too. – GopherGopher Mar 05 '20 at 02:53

2 Answers2

1

Pay attention here:

        for (int value : array1) {
            for (int i : array2) {
                if (value < i) {
                    temp = value;
                    counter++;
                    combinedArray = append(combinedArray, counter, temp);
                }
            }
        }

this loops 25 times (5 iterations over array2 for every of 5 elements in array1). This should already warn you. Another issue: during 5 first iterations you put "1" (because it satisfies if clause) into out array 5 times. Try debugging or at least some troubleshooting system.out.println

like:

        for (int value : array1) {
            for (int i : array2) {
                if (value < i) {
                    temp = value;
                    counter++;
                    System.out.println("value: " + value);
                    System.out.println("i: " + i);
                    System.out.println("counter: " + counter);
                    System.out.println("---------------");
                    combinedArray = append(combinedArray, counter, temp);
                }
            }
        }
// output:
value: 1
i: 2
counter: 1
---------------
value: 1
i: 4
counter: 2
---------------
value: 1
i: 6
counter: 3
---------------
value: 1
i: 8
counter: 4
---------------
value: 1
i: 10
counter: 5
---------------
value: 3
i: 4
counter: 6
---------------
value: 3
i: 6
counter: 7
---------------
value: 3
i: 8
counter: 8
---------------
value: 3
i: 10
counter: 9
---------------
value: 5
i: 6
counter: 10
---------------
value: 5
i: 8
counter: 11
---------------
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10

this shows all issues with implemented logic.

Something like below should work for you. Please pay attention on bounds checks for arays

import java.util.Arrays;

public class Methods2 {

    static int[] combine(int[] array1, int[] array2) {
        int[] combinedArray = new int[array1.length + array2.length];
        int index1 = 0;
        int index2 = 0;
        for (int i = 0; i < combinedArray.length; i++) {
            if (index1 < array1.length && index2 < array2.length) {
                // element left in both arrays
                if (array2[index2] < array1[index1]) {
                    combinedArray[i] = array2[index2];
                    index2++;
                } else {
                    combinedArray[i] = array1[index1];
                    index1++;
                }
            } else if (index1 < array1.length) {
                // element left in 1 array only
                combinedArray[i] = array1[index1];
                index1++;
            } else {
                // element left in 2 array only
                combinedArray[i] = array2[index2];
                index2++;
            }
        }
        return combinedArray;
    }

    public static void main(String[] args) {
        int[] array1 = {1, 3, 5, 7, 9};
        int[] array2 = {2, 4, 6, 8, 10};
        System.out.println(Arrays.toString(combine(array1, array2)));
    }
}
Makhno
  • 441
  • 2
  • 6
  • Wow, thanks for the ultra detailed answer. Solved my problem. Will use the debug method next time. – dizmac Mar 06 '20 at 16:04
0

It's a value passing and reference passing problem. The counter-- in append() will not affect the other counter in combine(). Btw, you can use two pointer method to solve merge two sorted array problem, not just one counter param. If you have no idea, you can check this solution github.

彭泽鑫
  • 99
  • 1
  • 2
  • 8