ArrayIndexOutOfBoundException,同时在JAVA中将奇数与数组分开

时间:2018-08-12 07:41:52

标签: java arrays indexoutofboundsexception

我正在尝试提出一种解决方案,以仅对数组中的奇数进行排序,同时保持偶数不变。为了做到这一点,我试图将给定数组中的所有奇数都删除到一个新的数组(odd_arr)中,并填补了这个空缺,我插入了一个大数(9731),以便我知道应该在哪里插入一旦对这些奇数进行了排序。

(只是为了理解Ex:如果数组为{5,3,2,8,1,4},则 step1 :odd_arr将为{5,3,1},数组是{9731,9731,2,8,9731,4} 第2步:已排序的odd_arr为{1、3、5} step3 :最后,在将主数组中的排序后的奇数替换为数字'9731'之后,输出应该是{1,3,2,8,5,4})。

这是我的代码,它提供ArrayIndexOutOfBoundException:

class Test {

public static int[] sortArray(int[] array) {

int[] odd_arr = new int[50];
int k =0;


for(int i :array){
  if(array[i] % 2 == 1)//Exception producing at this line{
    odd_arr[k] = array[i];
    array[i] = 9731;

    k = k+1;

  }
}    

Arrays.sort(odd_arr);   


int j=0;
for(int i:array){
  if(array[i] == 9731){
    array[i] = odd_arr[j];
    j = j+1;
  }
}    
return array;
}



public static void main(String[] args) {
int[] array = {5, 3, 2, 8, 1, 4};

int[] sorted_array = sortArray(array); //Exception here too

for(int i=0; i<sorted_array.length;i++)
  System.out.print(sorted_array[i] + " ");
 }
}

2 个答案:

答案 0 :(得分:1)

您将for-each循环视为常规循环

for(int i : array){
    if(array[i] % 2 == 1)

应该是

for(int i :array){
    if(i % 2 == 1)

但是,我实际上会将其分解为几种方法,以使其更易于推理。从计算赔率的方法开始。喜欢,

private static int countOdds(int[] array) {
    int count = 0;
    for (int val : array) {
        if (val % 2 != 0) {
            count++;
        }
    }
    return count;
}

在Java 8+中,也可以像这样

private static int countOdds(int[] array) {
    return (int) Arrays.stream(array).filter(i -> i % 2 != 0).count();
}

然后是一种将奇数值复制到新的(临时数组)的方法,例如

private static int[] copyOdds(int[] array) {
    int pos = 0;
    int[] odds = new int[countOdds(array)];
    for (int val : array) {
        if (val % 2 != 0) {
            odds[pos++] = val;
        }
    }
    return odds;
}

或者,在Java 8+中,

private static int[] copyOdds(int[] array) {
    return Arrays.stream(array).filter(i -> i % 2 != 0).toArray();
}

然后您的sortArray方法实际上会编写自己。首先,复制奇数值。然后对它们进行排序。然后将它们复制回原始阵列。喜欢,

public static void sortOdds(int[] array) {
    int[] odds = copyOdds(array);
    Arrays.sort(odds);
    int pos = 0;
    for (int i = 0; i < array.length; i++) {
        if (array[i] % 2 != 0) {
            array[i] = odds[pos++];
        }
    }
}

另外,为了演示一个main方法

public static void main(String[] args) {
    int[] array = { 5, 3, 2, 8, 1, 4 };
    System.out.println(Arrays.toString(array));
    sortOdds(array);
    System.out.println(Arrays.toString(array));
}

哪个输出

[5, 3, 2, 8, 1, 4]
[1, 3, 2, 8, 5, 4]

答案 1 :(得分:0)

首先,尝试使用正确的缩进,以便于阅读。

使用时:

for (int i : array) {}

变量'i'将是数组中的下一个变量,因此第一次是5,然后是3,依此类推。一次是8,当您尝试在长度为6的数组上调用array [8]时,它将返回错误。

您应该使用:

class Test {

public static int[] sortArray(int[] array) {

int[] odd_arr = new int[50];
int k =0;


for(int i = 0; i < array.length; i++){
  if(array[i] % 2 == 1)//Exception producing at this line{
    odd_arr[k] = array[i];
    array[i] = 9731;

    k = k+1;

  }
}    

Arrays.sort(odd_arr);   


int j=0;
for(int i = 0; i < array.length; i++) {
  if(array[i] == 9731){
    array[i] = odd_arr[j];
    j = j+1;
  }
}    
return array;
}



public static void main(String[] args) {
int[] array = {5, 3, 2, 8, 1, 4};

int[] sorted_array = sortArray(array); //Exception here too

for(int i=0; i<sorted_array.length;i++)
  System.out.print(sorted_array[i] + " ");
 }
}