Index out of bounds Java

MaxPan :

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.

Makhno :

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)));
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27165&siteId=1