简体   繁体   中英

ArrayIndexOutOfBoundExceptions on certain inputs when sorting elements of an array?

My task is to write a function that rearranges an array so that the odd numbers occur in the beginning of the array, from greatest to least, and the even numbers from least to greatest at the end. We are not allowed to use any other libraries except for the standard input and output streams.

The output works when the numbers are:

{-15, 450, 6, -9, 54}

But if I changed the elements to:

{-55, 45, 6, 11, 54}

There is an exception error. Here is my code:

public class ary1 {
    public static void sort(int A[], int n) {
        int tmp;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (A[0] % 2 == 0) //even
                {
                    if (A[i] < A[j]) {
                        tmp = A[i];
                        A[i] = A[j];
                        A[j] = tmp;
                    }
                } else {
                    if (A[i] > A[j]) {
                        tmp = A[i];
                        A[i] = A[j];
                        A[j] = tmp;
                    }
                }
            }
        }
    }

    public static void showAray(int A[], int n) {
        for (int i = 0; i < n; i++) {
            System.out.println(A[i]);
        }
    }

    public static void main(String args[]) {
        int array1[] = {-55, 45, 6, 11, 54};
        int odd = 0;
        int even = 0;

        for (int i = 0; i < array1.length; i++) {
            if (array1[i] % 2 == 0) {
                even++;
            } else {
                odd++;
            }
        }

        int[] array2 = new int[even];
        int[] array3 = new int[odd];

        for (int i = 0, j = 0, k = 0; i < array1.length; i++) {
            if (array1[i] % 2 == 0) {
                array2[j++] = array1[i];
            } else {
                array3[k++] = array1[i];
            }
        }

        System.out.println("Original array:\n");
        showAray(array1, array1.length);

        sort(array2, even);
        sort(array3, odd);

        for (int i = 1; i < array1.length; i++) {
            if (i < odd) {
                array1[i] = array3[i];
            } else {
                array1[i] = array2[(i + 1) - even];
            }
        }

        System.out.println("\nAfter sorting:\n");
        showAray(array1, array1.length);
    }
}

I know there is a logical error here, but I can't figure out what exactly. Is there any way to change the logic to work with all integers? Thanks.

array1[i] = array2[(i + 1) - even];

EDIT - Here is the stacktrace.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at ary.main(arytest.java:67)
Java Result: 1

Change this

array1[i] = array2[(i + 1) - even];

to

array1[i] = array2[i - odd];

I guess this is what you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM