【问题标题】:NullPointerException: radixSortNullPointerException:基数排序
【发布时间】:2021-08-09 12:06:02
【问题描述】:
import java.lang.Math;




public class radixSort{
    public static int getFreeSpace(Integer array[]){        //this function gets the first occurence of a null element in the array
        int i;
        for (i = 0; i < array.length;i++){
            if (array[i] == null){
                break;
            }
        }
        return i;
    }
    public static Integer[] flatten(Integer array[][]){    //this function flattens the multi-dimensional lsdArray to a single dimension array
        Integer finalArray[] = new Integer[1000];
        for (int i = 0; i < array.length; i++){
            Integer[] currentArray = array[i];
            for (int j = 0; j < currentArray.length; j++){
                if (array[i][j] != null){
                    int index = getFreeSpace(finalArray); 
                    finalArray[index] = currentArray[j];
                                        
                }
                else;     
                                         
            }

            
            
        }
        return finalArray;
    }
    public static Integer getMaxDigits(Integer array[]){    //this function gets the largest number of digits in the array. this is helplful for getting the number of times the array is passed into the sort
        Integer max = 0;
        Integer m = 0;
        for (int i = 0; i < array.length; i++){
            m = Math.max(max, array[i]);
        }
        String s = m.toString();
        return s.length();
    }

    public static Integer[] radixSortFunc(Integer array[],Integer maxDigits, Integer counter){ //the sorting algorithm
        Integer[][] lsdArray = new Integer[10][15];
        if (counter <= maxDigits){
            
            for (Integer i = 0; i < array.length; i++){
                double temp = Math.pow(10,counter+1);
                int powOfTen = (int) temp;
                int term = (array[i] % powOfTen );
                temp = Math.pow(10,counter);
                powOfTen = (int) temp;
                int digit = Math.floorDiv(term,powOfTen);
                                  
                Integer[] currentArray = lsdArray[digit];
                Integer index = getFreeSpace(currentArray);
                currentArray[index] = array[i];
                lsdArray[digit] = currentArray;


                
    
                }
        
            counter++;           
            return radixSortFunc(flatten(lsdArray),maxDigits,counter);
        }
        else{
            return array;
        }
        

    }

    public static void main(String[] args){
        Integer[] array ={12,23,45,23,166};
        Integer max = getMaxDigits(array);
        Integer[] sortedArray = radixSortFunc(array,max,0);
        for (Integer i = 0; i < sortedArray.length; i++){
            if (sortedArray[i] != null){
                System.out.println(sortedArray[i]);
            }
            else;
            
        }
        

    }
}

我是java的初学者。我已经制作了这段代码来使用基数排序对数组进行排序。当我使用任何测试数组运行代码时,我会收到此错误:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "array[java.lang.Integer.intValue()]" is null
        at radixSort.radixSortFunc(radixSort.java:52)
        at radixSort.radixSortFunc(radixSort.java:68)
        at radixSort.main(radixSort.java:80)

我是java的初学者,所以如果我犯了一些菜鸟错误,请不要取笑我。 如果你给我一个更短的方法来做这件事也会很有帮助。例如减少函数的数量。

【问题讨论】:

    标签: java radix-sort


    【解决方案1】:

    因为你打电话

    return radixSortFunc(flatten(lsdArray),maxDigits,counter);

    在第一个循环之后。

    lsdArray 是一个几乎充满空值的数组(因为你没有在Integer[][] lsdArray = new Integer[10][15]; 中初始化它们)。

    因此,当您通过 array[i] = null 传入 int term = (array[i] % powOfTen ); 时,您将获得 NPE。

    顺便说一句,您的getMaxDigits 也有问题,它没有返回数组的最大长度元素。

        for (int i = 0; i < array.length; i++){
            m = Math.max(max, array[i]);
        }
    

    它返回数组最后一个元素的长度。相反,您应该这样做

        for (int i = 0; i < array.length; i++){
            max = Math.max(max, array[i]);
            m = max;
        }
    

    如果你想优化你的功能,你也可以删除 var m 并且只使用 max :)

    【讨论】:

    • 非常感谢。在第二个问题中,我想要数组元素的长度而不是最大元素。但还是非常感谢你
    猜你喜欢
    • 2020-05-30
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    相关资源
    最近更新 更多