java数组排序偶数和奇数整数的超出范围异常?

时间:2015-05-20 08:31:57

标签: java arrays sorting

错误发生在odd[count1] = value

这个程序基本上应该打印一个2d array,其中evens小于赔率,然后从最低到最高排序。

public static void main(String args[]) {
        int[][] arzarehard = {{12,13,17}, {38,44,13}, {54,37,15}, {35,25,17}};
        oddSort(arzarehard);
}

  public static void oddSort(int[][] thots) {
    int [] even = new int[thots.length + thots[0].length];
    int [] odd = new int[thots.length + thots[0].length];
    for (int i=0; i<even.length; i++) {
        even[i] = Integer.MAX_VALUE;
    }
    for(int i=0; i<odd.length; i++) {
        odd[i] = Integer.MAX_VALUE;
    }
    int count = 0;
    int count1 = 0;
    //try non for each - possibly causing problem
    for (int[] row : thots) {
        for(int value : row) {
            if (value%2==0) {
                even[count] = value;
                count++;
            } else {
                //odd.add(value); - adds it to the end and then     concatinate
                odd[count1] = value;
                count1++;
            }
        }
    }
    //even bubble sort 
    for(int j=0; j<odd.length; j++) {
        for(int i=0; i<odd.length-1; i++) {
            if(odd[i]>odd[i+1]) {
                int temp = odd[i];
                int tempTwo = odd[i+1];
                odd[i] = tempTwo;
                odd[i+1] = temp;
            }
        }
    }
    //odd bubble sort
    for(int j=0; j<even.length; j++) {
        for(int i=0; i<even.length-1; i++) {
            if(even[i]>even[i+1]) {
                int temp = even[i];
                int tempTwo = even[i+1];
                even[i] = tempTwo;
                even[i+1] = temp;
            }
        }
    }

    int e = 0;
    int o = 0;

    for(int j=0; j<thots.length; j++) {
        for(int i=0; i<thots[0].length; i++) {
            if(e<even.length) {
                thots[j][i] = even[e];
                e++;
            } else {
                thots[j][i] = odd[o];
                o++;
            }
        }
    }

    for(int[] whatever : thots) {
        for( int value : whatever) {
            System.out.print(value + " ");

        }
        System.out.println();
    }  
    }

基本思想是输入2d array。然后将该数组拆分为偶数和奇数数组。然后对它们进行分类并将它们放回原处进行打印。

2 个答案:

答案 0 :(得分:1)

因为您的数组even[]odd[]的代码大小为7。它应足以容纳所有值。当您将值17赋予{时{1}}这将通过odd[7]

更改代码 -

ArrayIndexOutOfBoundException

到 -

int [] even = new int[thots.length + thots[0].length];
int [] odd = new int[thots.length + thots[0].length];

答案 1 :(得分:0)

使用以下代码 -

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class BinarySearch{



    public static void main(String args[]) {
        //System.out.println(Integer.MAX_VALUE);
        int[][] arzarehard = {{12,13,17}, {38,44,13}, {54,37,15}, {35,25,17}};
        oddSort(arzarehard);
}

  public static void oddSort(int[][] thots) {

    List<Integer> evenList=new ArrayList<Integer>();
    List<Integer> oddList=new ArrayList<Integer>();
    for (int[] row : thots) {
        for(int value : row) {
            if (value%2==0) {
                evenList.add(value);
            } else {
                oddList.add(value);
            }
        }
    }
    Collections.sort(evenList);
    Collections.sort(oddList);
    int i=0;
    int j=0;



 for(Integer even:evenList){
     if(j==thots[0].length){
         i++;
         j=0;
     }
     thots[i][j]=even;
     j++;
 }
 for(Integer odd:oddList){
     if(j==thots[0].length){
         i++;
         j=0;
     }
     thots[i][j]=odd;
     j++;
 }
    for(int[] whatever : thots) {
        for( int value : whatever) {
            System.out.print(value + " ");

        }
        System.out.println();
    }  
    }
}