为什么Arrays.sort()在我的arrayB上不起作用?主方法中的java.lang.NullPointerException也有问题

时间:2019-04-16 05:33:22

标签: java arrays sorting timing

我目前正在一个项目中,我需要使用随机生成的整数创建一个初始数组(anArray),并在另一个数组(arrayB)中精确复制这些随机值。然后,我想使用我的selectionSort()方法对第一个数组进行排序,并使用Arrays.sort()对第二个数组进行排序。我还需要确定每种排序方法需要多长时间。

到目前为止,我可以用随机整数填充第一个数组。我不确定我是否已正确地将anArray复制到arrayB中,因为到目前为止我所编写的代码都无法打印。 anArray根据需要进行排序,并且通过main进行打印没有问题。请到目前为止检查我的代码,如果您能建议在哪里添加时间来安排排序过程,那将也很棒!

for (i=1;i<=28;i++) {
  for j in (start,end) {
     for k in (a,b,c,d,e,f) {
            file = 'chr' + $i + '_' + $j + '_' + $k;
                     }}}

因此,我希望生成一个按顺序排序的anArray打印清单,并记录对anArray进行排序所花费的时间。

然后,使用arrayB作为anArray中随机整数的精确副本,使用Arrays.sort()对arrayB进行排序,并记录排序时间。

2 个答案:

答案 0 :(得分:0)

您的NPE是由于未将arrayB分配给阵列副本的输出引起的。

尝试以下代码:

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

public class TimedSortOne
{

    public static int[] anArray; // initializes the first array
    public static int[] arrayB; // initializes the second array

    /**
      * This method produces integers of random values.
      * @return int randomNum
      */
    private static int randomFill()
    {       
        Random rand = new Random();
        int randomNum = rand.nextInt();
        return randomNum;        
    }

    private static int[] list()
    {
        anArray = new int[1000];
        for(int i=0;i<anArray.length;i++)
        {
            anArray[i] = randomFill();
        }
        return anArray;
    }

    /**
     * This method sorts the values of anArray into ascending order by 
     * repeatedly finding the largest value and moving
     * it to the last index in the array.
     * @param int[] anArray
     */
    private static void selectionSort(int[] anArray)
    {

        for (int lastPlace = anArray.length-1; lastPlace > 0; lastPlace--)
        {
            int maxLoc = 0;

            for (int j = 1; j <= lastPlace; j++)
            {
                if (anArray[j] > anArray[maxLoc]) 
                {
                    maxLoc = j;
                }
            }

            int temp = anArray[maxLoc];
            anArray[maxLoc] = anArray[lastPlace];
            anArray[lastPlace] = temp;

        }
    }


    /**
     * This method populates arrayB with an exact copy
     * of integer values from anArray using System.arraycopy() function.
     * @param anArray
     * @return arrayB
     */
    private static int[] arrayCopyFull(int[] anArray)
    {
        int[] temp = new int[anArray.length];
        System.arraycopy(anArray, 0, temp, 0, anArray.length);
        return temp;
    }

    public static void main(String[] args)
    {

        list();
        arrayB = arrayCopyFull(anArray); //forgot to assigned to arrayB

        long startTime = new Date().getTime();
        selectionSort(anArray);
        long endTime = new Date().getTime();
        System.out.println("Time taken for selectionSort(): " + (endTime - startTime) + " seconds.");


        startTime = new Date().getTime();
        Arrays.sort(arrayB);
        endTime = new Date().getTime();
        System.out.println("Time taken for Arrays.sort(): " + (endTime - startTime) + " seconds.");


        System.out.println("The sorted integers in  anArray are:");
            for (int numbers : anArray) {
             System.out.println(numbers);
            }

        System.out.println("The sorted integers in arrayB are:");
            for (int bNumbers : arrayB) {
               System.out.println(bNumbers);
            } 

    }

}

答案 1 :(得分:0)

在您的代码中,arrayB从未初始化。在“ arrayCopyFull()”方法中使用“ arrayB”代替“ temp”。这样可以解决NPE错误。

要计算时间,请点击链接here。它具有可用于添加时间的详细方法。

相关问题