簡體   English   中英

java數組對偶數和奇數整數排序時超出范圍的異常?

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

錯誤發生在odd[count1] = value

這個程序基本上應該打印一個2d array ,偶數小於幾率,然后從最低到最高排序。

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 然后將該數組分解為偶數和奇數數組。 然后將它們分類並放回一起打印。

因為在您的代碼中array even[]odd[]7它應該足以容納所有值。當將17賦給odd[7]這將通過ArrayIndexOutOfBoundException

更改代碼

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

至-

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

使用以下代碼-

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM