当数组的大小时没有立即分配时获取 NullPointerException

Getting NullPointerException in when array's size is not instantly assigned

我正在努力学习 Java,我目前正在编写一个程序,它将一个整数数组拆分为两个子数组,一个包含正值,另一个包含负值。

由于无法从一开始就指定两个子数组的大小(因为这个程序应该适用于任何整数数组)我写了两个方法来计算两个子数组的大小-数组(maxNeg 和 maxPos)。之后,两个子数组(arrayNegative 和 arrayPositive)被初始化为具有两个相应的大小。

问题是,当我尝试使用 arraySorter() 填充两个数组时,编译器在 arraySorter() 方法内的第一次迭代时给我 ArrayIndexOutOfBoundsException: 0 错误。

注意 1:从一开始就将值分配给 maxNeg 和 maxPos 变量时不会出现此问题。

注意 2:我知道这类问题通常使用 ArrayLists 来存储正负数来解决,但我的作业要求迫使我只能使用数组来做到这一点。

public class sortMethod {
public int max; // max = main array length
public int maxNeg; // the length of the final array that will store the
                    // negative integers
public int maxPos; // the length of the final array that will store the
                    // positive integers

public int[] intArray = new int[max];

public int getMaxNeg() {
    for (int i = 0; i < max; i++) {
        if (intArray[i] < 0) {
            maxNeg++;
        }
    }
    return maxNeg;
}

public int getMaxPos() {
    for (int i = 0; i < max; i++) {
        if (intArray[i] >= 0) {
            maxPos++;
        }
    }
    return maxPos;
}
public int[] negativeArray = new int[maxNeg];
public int[] positiveArray = new int[maxPos];
public int negIndex = 0;
public int posIndex = 0;

public void arraySorter() {
    for (int a = 0; a < max; a++) {
        if (intArray[a] < 0) {
            negativeArray[negIndex] = intArray[a];
            negIndex++;
            // System.out.println(negativeArray[0]);
        } else {
            positiveArray[posIndex] = intArray[a];
            posIndex++;
        }
    }
}

public sortMethod(int[] y, int z) {
    this.intArray = y;
    this.max = z;

}

有人可以解释为什么我在使用公式计算两个子数组的大小时一直收到 NullPointerException 以及为什么在声明它们时将值分配给 2 个变量时我没有收到错误。

这是我创建测试对象的主要方法:

public class ArraySort {

public static void main(String[] args) {
    int[] array = { -12, 23, -22, 0, 43, -545, -4, -55, -43, -12, 0, -999};

    int z = array.length;
    sortMethod arrayTest = new sortMethod(array, z);

    arrayTest.getMaxNeg();
    arrayTest.getMaxPos();
    arrayTest.arraySorter();

}

谢谢,如果我的问题格式不符合网站标准,请原谅,这是我的第一次,我会在以后努力改进。

在 Java 中定义变量和方法的顺序并不重要。您的实例变量(例如您的 negativeArray 和 positiveArray)在创建 class 实例后立即创建(并且在调用构造函数之前)。因此,您将两个数组都初始化为 0.

执行这段代码时

public int[] negativeArray = new int[maxNeg];
public int[] positiveArray = new int[maxPos];

maxNegmaxPos等于0,所以出现越界异常是正常的。

要使其正常工作,您可以直接在方法中初始化数组,这样您的代码将变为:

public int[] negativeArray;
public int[] positiveArray;
public int negIndex = 0;
public int posIndex = 0;


public void arraySorter() {
    negativeArray = new int[maxNeg];
    positiveArray = new int[maxPos];
    for (int a = 0; a < max; a++) {
        if (intArray[a] < 0) {
            negativeArray[negIndex] = intArray[a];
            negIndex++;
        } else {
            positiveArray[posIndex] = intArray[a];
            posIndex++;
        }
    }
}

您的代码的另一部分错误是 sortMethod 的构造函数。您将数组长度赋予最大界限,但最大界限实际上是长度 - 1。因此将 this.max = z; 更改为 this.max = z - 1;

此外,您的算法不会对数字进行排序,它只是将正负数分开...如果您想对一组数字进行排序,可以将其简化为一行:

Arrays.sort(array);