简体   繁体   English

在对数组元素进行排序时某些输入上的ArrayIndexOutOfBoundExceptions吗?

[英]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. 编辑-这是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 我想这就是你想要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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