找不到 ArrayIndexOutOfBoundsException [duplicate] 的源代码

Can't find the source of ArrayIndexOutOfBoundsException [duplicate]

提问人:stupidmoron 提问时间:10/11/2022 更新时间:10/11/2022 访问量:34

问:

我知道还有其他关于 ArrayIndexOutOfBoundsException 的帖子,但我仍然无法确定此异常发生的位置。我已经为这只野兽工作了整整一个星期。我现在对一切都视而不见,因此需要一双新的眼睛来窥视它。这是我的代码,带有错误消息:

import java.util.Arrays;
import java.util.Random;

public class BucketSort {
    
    //Random rand = new Random();
    //int length = 3 + rand.nextInt(10);
    int[] array = {97, 100, 3, 12475, 1500, 90};
    int[][] buckets = new int[10][array.length - 1];
    int col = 0;
    int row = 0;
    boolean occupied = false;
    int max = 0;
    int maxDigits = 1;
    
    public BucketSort() { // constructor
        for (int i = 0; i < array.length; i++) {  // for each array element
            //array[i] = rand.nextInt(1000);
            System.out.print(array[i] + " ");
            if (array[i] > max)  // if current array element is greater than max
                max = array[i];  // set the new max
            System.out.println("\nMax is: " + max);
        }
        // get the number of digits in the largest number (max) in the array 
        int largest = max;  // largest is a temp var initialized to the value of max.  I'm using it in calculations because I don't wanna destroy max
        while (largest / 10 > 0) { // the largest number in the array is more than one digit
            maxDigits++;  // add 1 to maxDigits
            largest /= 10; // divide largest by 10 
            System.out.println("maxdigits is: " + maxDigits);
            System.out.println("Largest is: " + largest);
        }
    }
    
    public void distribPass() {
        // distribution pass
        for (int i = 0; i < array.length; i++) {  // for each array element
            row = array[i] % 10;  // get the ones place
            if (buckets[row][col] == 0) { // if bucket is not occupied
                buckets[row][col] = array[i];  // store the array element in bucket
            }
            else {
                occupied = true;  // bucket is occupied
                while (occupied && (col < array.length - 1)) {  // bucket is occupied
                    col++;  // move to the next column
                    if (buckets[row][col] == 0 ){  // the bucket in the new column is not occupied
                        buckets[row][col] = array[i];  // store the array element in the bucket
                        occupied = false;  // set occupied to false
                    } // end if
                } // end while
            } // end else
            
        } // end for
        
        for (row = 0; row < buckets.length; row++) {  // for each row in buckets
            //System.out.println("row: " + row + "\n");
            System.out.println();
            for (col = 0; col < buckets[row].length; col++) {  // for each column in current row
                System.out.print(buckets[row][col] + "\t");  // print contents of the current bucket
            }
        }
    } // end distribPass
    
    public void gatherPass() {
        int index = 0;  // reset the array index to 0
        System.out.println("Gathering Pass");
        for (row = 0; row < buckets.length; row++) {  // for each row in buckets
            for (col = 0; col < buckets[row].length; col++) {  // for each column of current row
                if (buckets[row][col] != 0) {  // there's a number in the current bucket
                    array[index] = buckets[row][col];  // copy the number from the bucket back into the array
                    System.out.println(array[index]);
                    index++;  // move to the next position in the array
                    buckets[row][col] = 0;  // reset current bucket to 0
                }
            }
        }
    }
    
    public void sort() {
        // tens digit, hundreds digit, etc.
        row = 0;
        col = 0;
        System.out.println("Testing the sort method");
        for (int digit = 2; digit <= maxDigits; digit++) {  // for each digit of the current array element
        //for (int i = 0; i < array.length; i++) {  // for each array element
            for (int i = 0; i < array.length - 1; i++) {  // for each array element
                row = getPlace(array[i], digit - 1);  // get the proper bucket row to store the current array element in
                System.out.println("Digit is: " + digit + " Place is " + row);
                
                // do another distribution pass
                if (buckets[row][col] == 0) {  // current bucket is not occupied
                    buckets[row][col] = array[i];  // store the current array item in the current bucket
                }
                else {  // current bucket is occupied
                    occupied = true;  // set occupied to true
                    while (occupied && (col < array.length - 1)) {
                        col++;  // move to the next column
                        if (col >= array.length - 1)
                            break;
                        if (buckets[row][col] == 0) {  // if current bucket is not occupied
                            buckets[row][col] = array[i];  // store the current array item in the current bucket
                            occupied = false;  // set occupied to false
                        }
                    }
                }
            }
            
        }
        gatherPass();
    }
    
    public int getPlace(int num, int exp) {
        int intPlace = 0;
        double doublePlace;
        if (num >= Math.pow(10, exp) && num < Math.pow(10, exp + 1)) {  // for example, to check if a number is between 10 and 100
            // base case
            doublePlace = num / Math.pow(10, exp);
            intPlace = (int) doublePlace;
            //return intPlace;
        }   
        else if (num < Math.pow(10, exp))  // for example, if the number is less than 10
            intPlace = 0;
        else if (num >= Math.pow(10, exp + 1))  // for example, if the number is greater than 100
            intPlace = getPlace(num / 10, exp);  // recursive call
        return intPlace;
    }
        
}

public class BucketSortTest {
    public static void main(String[] args) {
        BucketSort screwup = new BucketSort();
        screwup.distribPass();
        screwup.gatherPass();
        screwup.sort();
    }
}

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
        at BucketSort.sort(BucketSort.java:90)
        at BucketSortTest.main(BucketSortTest.java:6)
Java 数组 ArrayIndexOutofboundsException

评论

1赞 Dawood ibn Kareem 10/11/2022
它位于第 90 行。堆栈跟踪会告诉您这一点。

答:

0赞 Dhaval Gajjar 10/11/2022 #1

由于您编写的逻辑正在计算 col > 5,因此引发了错误。您的存储桶大小为 5,并且您让列在该大小之外使用,因此它会引发错误。if (buckets[row][col] == 0) {

检查周围的逻辑,确保它不会超出大小。col++; // move to the next column