Search code examples
javaarrayswindowsintegerlogic

Why does my Int value of an int array become 0 when it is higher than 10?


I wrote code which sorts the values of two arrays in ascending order and it actually works but only if I use values lower than 10 in the arrays. It actually sounds like a simple problem, but I already tried anything and I just don't understand why it isn't working.

First I should show you the merge function I wrote:

public static int[] merge(int[] leftArray, int[] rightArray){
    int[] result = new int[leftArray.length + rightArray.length];
    int position = 0;
    int left = 0;
    int right = 0;

    String result_debug;
    while(true){
        /*
        System.out.println("Result: " + result[position]);
        System.out.println("Left: " + leftArray[left]);
        System.out.println("Right: " + rightArray[right]);
         */
        if(leftArray[left] < rightArray[right]){
            result[position] = leftArray[left];

            position = position + 1;
            result_debug = arrayToString(result);
            System.out.println(result_debug);
            left++;
        } else if (leftArray[left] > rightArray[right]) {
            result[position] = rightArray[right];
            position = position + 1;
            result_debug = arrayToString(result);
            System.out.println(result_debug);
            right++;

        } else if (leftArray[left] == rightArray[right]) {
            result[position] = leftArray[left];
            result_debug = arrayToString(result);
            System.out.println(result_debug);
            position = position + 1;
            left++;
            result[position] = rightArray[right];
            result_debug = arrayToString(result);
            System.out.println(result_debug);
            position = position + 1;
            right++;
        }

        if(left == leftArray.length && right < rightArray.length) {

            for(int i = position; i < rightArray.length; i++){
                result[i] = rightArray[right];
                System.out.println(rightArray[right]);
                right++;
            }
            break;
        } else if (right == rightArray.length && left < leftArray.length) {

            for(int i = position; i < leftArray.length; i++){
                result[i] = leftArray[left];
                System.out.println(leftArray[left]);
                left++;
            }
            break;
        } else if (left == leftArray.length && right == rightArray.length) {

            break;


        }

        //System.out.println("Result: " + result[position]);
        //System.out.println("Left: " + leftArray[left]);
        //System.out.println("Right: " + rightArray[right]);
    }


    System.out.println("Result: " + result[result.length - 1]);
    
    return result;
}

Anytime I use arrays with values below 10, it works just perfectly, but when I use anything higher it becomes 0. As you can see I already tried some debugging methods but without success. I also wrote a function to make an int array into a String to print it out I can show you that function too:

public static String arrayToString(int[]arr){
    String result = "[" + arr[0];

    for(int i = 1; i < arr.length; i++){
        result = result + ", " + arr[i];
    }
    result += "]";
    return result;
}

I also thought it could be because of the arraytoString method, but as you can see at the bottom of my merge method, I already checked that and the output is still 0, so it isn't because of that.

I don't think that it is a logic problem because as I said as long as I use values below 10 it works and also, at the beginning when I programmed it, it didn't even work with values higher than 8 it later began to work with 9 and 10.

I am using Windows 10 Pro.


Solution

  • the problem is not with array values, but rather with the placement of values in the two arrays. the problem will be manifested with the input

        int[] a = {1,2,3};
        int[] b = {4,5,6};
        int[] c = merge(a, b);
    

    you should learn how to use debugger. then you can debug your code properly. The problem is that the first item in array b is larger than all items in array a. so you populate result with all items from a and advance left (and also position) until left is equal to leftArray.length.

    in the above example, at this point, both left and position are equal 3 .

    at that point you enter this if statement: if(left == leftArray.length && right < rightArray.length) {
    you assign i to start with value position. making it equal to rightArray.length and thus you do not copy over array b.