为什么这会给我 NullpointerException 错误?它发生在我的代码的第三行,所以我的第一个 for 循环。正如您所知,这是一种可以访问较大代码的其他部分的方法。
public static void insertionsort()
{
for (int outer = 1; outer < array.length; outer++)//Outer=next number to be sorted
{
String temp = array[outer]; //Stores it for later use
int inner = outer; // inner used to track shifts
while (inner > 0 && array[inner - 1].compareTo(temp) >= 0)
{
array[inner] = array[inner - 1];//Swaps the number
inner--;// Decrements
} //shift them all right until one is smaller
array[inner] = temp;//Now it will put the stored number into its ordered position.
}
}
最佳答案
该行中 undefined variable array
for (int outer = 1; outer < array.length; outer++)//Outer=next number to be sorted
如果这是一个函数参数,则像这样添加它
public static void insertionsort(int[] array)
{
for (int outer = 1; outer < array.length; outer++)//Outer=next number to be sorted
或者在里面定义它
public static void insertionsort()
{
int[] array = {2,1,4,7,6,3};
for (int outer = 1; outer < array.length; outer++)//Outer=next number to be sorted
关于java - 它告诉我 NullPointerException 的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20056710/