简体   繁体   中英

Out of bounds exception on java array sorting even and odd integers?

The error occurs at odd[count1] = value .

This program should basically print a 2d array with evens being less than odds and then both sorted from lowest to highest.

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();
    }  
    }

The basic idea is that I am inputting a 2d array . Then breaking that array into an even and odd array. Then sorting both and putting them back together to print.

since in you code size of array even[] and odd[] is 7 .it should be sufficient enough to hold all the values.when you assigh value 17 to odd[7] this will through ArrayIndexOutOfBoundException .

change code-

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

to-

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

Use following code-

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();
    }  
    }
}

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