两个快速修复问题 - 合并排序

two quick fix issues - Merge Sort

我已经在 java 代码中创建了我的合并排序算法版本。我的问题是:当我按原样 运行 代码时,我在第 27 行的主代码中得到一个 NullPointerExecpetion(请参阅注释行)。而且我知道有办法调用方法并实例化 newArray 而不是静态的,但我不太确定如何......有人可以帮助解决这些问题吗?我对 java 还是比较陌生,所以请多关照 :)

主要:

import java.util.Random;

public class MergeSort_main 
{
public static void main(String[] args) 
{
    int[] originalArray = new int[1000];
    Random rand = new Random();

    for (int i = 0; i < originalArray.length; i++)
    {
        int randNum = rand.nextInt(1000)+1;
        originalArray[i] = randNum;
    }

    for(int i = 0; i < originalArray.length; i++)
    {
        System.out.println(i+"." + originalArray[i]);
    }

    System.out.println("---------------------End Random Array-------\n");

    MergeSortAlgorithm.mergeSortAlg(originalArray);
    int[] sortedArray = MergeSortAlgorithm.getSortedArray();


    for(int i = 0; i < sortedArray.length; i++) //NULL POINTER EXCEPTION HERE
    {
        System.out.println(i+ "." + sortedArray[i]);
    }

   }    
}

算法Class:

public class MergeSortAlgorithm 
{

private static int[] newArray;


 public static void mergeSortAlg(int[] randomNums) 
 {       
        int size = randomNums.length;

        if (size < 2)
        {
            return; //if the array can not be split up further, stop attempting to split. 
        }
        int half = size / 2;

        int firstHalfNums = half;
        int secondHalfNums = size - half;

        int[] firstArray = new int[firstHalfNums];
        int[] secondArray = new int[secondHalfNums];

        for (int i = 0; i < half; i++) 
        {
            firstArray[i] = randomNums[i];
        }

        for (int i = half; i < size; i++) 
        {
            secondArray[i - half] = randomNums[i];
        }
        mergeSortAlg(firstArray);
        mergeSortAlg(secondArray);
        merge(firstArray, secondArray, randomNums);
    }

    public static void merge(int[] firstArray, int[] secondArray, int[] newArray) 
    {
        int firstHalfNums = firstArray.length;
        int secondHalfNums = secondArray.length;
        int i = 0; //iterator for firstArray
        int j = 0; //iterator for second array
        int k = 0; //interator for randomNums array

        while (i < firstHalfNums && j < secondHalfNums) 
        {
            if (firstArray[i] <= secondArray[j]) 
            {
                newArray[k] = firstArray[i];
                i++;
                k++;
            } 
            else 
            {
                newArray[k] = secondArray[j];
                k++;
                j++;
            }
        }
        while (i < firstHalfNums) 
        {
            newArray[k] = firstArray[i];
            k++;
            i++;
        }
        while (j < firstHalfNums) 
        {
            newArray[k] = secondArray[j];
            k++;
            j++;
        }  


    }

    public static int[] getSortedArray()
    {
        return newArray;
    }


}

基本上,您的代码的唯一问题是您没有使用任何值初始化 newArray,从而导致 null.

您还在 merge 函数的顶部重新定义了 newArray

问题是 newArray[] 从未实例化,即 newArray 引用指向 null。并且,newArray 中没有任何更改,因此返回给 main 的值或引用为空。然后,您正在执行 sortedArray.length where sorted array having a null value.

你必须让 newArray[] 指向 randomNums[]。