两个快速解决问题 - 合并排序

时间:2015-11-25 03:44:44

标签: java nullpointerexception mergesort

我在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]);
    }

   }    
}

算法类:

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


}

2 个答案:

答案 0 :(得分:0)

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

您还在newArray功能的顶部重新定义merge

答案 1 :(得分:0)

问题是newArray []从未实例化,即newArray引用指向null。并且,newArray中没有更改,因此返回main的值或引用为null。然后,您正在执行sortedArray.length,其中排序数组具有空值。

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