Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在类中定义数组大小时,为什么会出现ArrayIndexOutOfBounds异常?_Java_Arrays_Class_Variables - Fatal编程技术网

Java 在类中定义数组大小时,为什么会出现ArrayIndexOutOfBounds异常?

Java 在类中定义数组大小时,为什么会出现ArrayIndexOutOfBounds异常?,java,arrays,class,variables,Java,Arrays,Class,Variables,对于我的项目,用户必须定义数组的大小,为此我创建了一个set方法。正在更改的变量是类变量arraySize。但是,当有人设置数组大小时,我尝试添加一个数组,我收到一个indexoutofbounds错误 下面是代码片段 //Size of the array private int arraySize; //initializes elemnt count of array private int arrayElementCount; //Gets count of SID in ar

对于我的项目,用户必须定义数组的大小,为此我创建了一个set方法。正在更改的变量是类变量arraySize。但是,当有人设置数组大小时,我尝试添加一个数组,我收到一个indexoutofbounds错误

下面是代码片段

    //Size of the array
private int arraySize;
//initializes elemnt count of array
private int arrayElementCount;

//Gets count of SID in array
public int getSIDArrayCount() {

    //returns private variable
    return arrayElementCount;
}

//Sets SID Array
public void setArraySize(int setArraySize) {
    //Array size int
    //checks array if array size is greater than 0
    if (setArraySize > 0) {
        //sets array size
           //SIDArray
         arraySize = setArraySize;
    }

}

//SIDArray
private String[] SIDArray = new String[arraySize];
编辑: 我无法在“setArraySize”方法中初始化数组,因为我需要数组的访问器方法

以下是本课程的其他内容:

/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 *要更改此模板文件,请选择工具|模板 *然后在编辑器中打开模板。 */ 包com.ahellhound.pkg202sid项目

导入java.util.array; 导入java.util.regex.Pattern

/** * *@作者科尔比·勒克莱尔 */ 公共类SidarayTest{

//Size of the array
private int arraySize;
//initializes elemnt count of array
private int arrayElementCount;

//Gets count of SID in array
public int getSIDArrayCount() {

    //returns private variable
    return arrayElementCount;
}

//Sets SID Array
public void setArraySize(int setArraySize) {
    //Array size int
    //checks array if array size is greater than 0
    if (setArraySize > 0) {
        //sets array size
           //SIDArray
         arraySize = setArraySize;
    }

}

//SIDArray
private String[] SIDArray = new String[arraySize];
//Gets SID Array
public String[] getSIDArray() {

    //returns array
    return SIDArray;
}

//Validates SID; returns true if SID entry is valid
public boolean validateSID(String SIDEntry) {
    //checks if sid entry is 7, and if starts with s
    if (SIDEntry.length() != 7) {
        //retuns false
        return false;

    }

    //checks if SIDEntry begins with 'S'
    if (!SIDEntry.matches("[sS]\\d{6}")) {

        //returns false
        return false;
    }
    //returns true
    return true;
}

//Adds SID to the Array
public void addSID(String SIDEntry) {

    //checks if SID is valid
    if (validateSID(SIDEntry)) {
        //Adds sid to array
        SIDArray[arrayElementCount] = SIDEntry;
        //increases array size by one
        arrayElementCount++;

    } else {

        System.out.println("test failed 2");
    }

}

//Gets SID array and returns all entrys to strings 
public String getSIDArrayString() {
    //Gets array and converts to string
    String SIDArrayString = Arrays.toString(SIDArray);
    //returns array string
    return SIDArrayString;

}
}


当我为数组创建了一个类变量,同时让数组成为一个“可获取”的方法时,如何设置数组大小确实让我感到困惑。

在调用setArraySize方法之前,您正在实例化数组,因此它的大小为0

//SIDArray
private String[] SIDArray = new String[arraySize];
由于SIDArray变量位于类的作用域中,因此在创建类的对象后,它将立即被实例化。因此,它使用arraySize变量,该变量此时未实例化(对于int,默认为0)

要解决此问题,只需在调用setArraySize()后实例化数组

这里有一些代码来澄清我在评论中的意思

public class A {
    /* In your code, you instantiate the array at this point, in the scope of the class, while arraySize is still uninitialized */
    String[] array; /* uninitialized, defaults to null */
    int arraySize; /* uninitialized, defaults to 0 */

    /* Simple accessor method for the array */
    public String[] getArray() {
        return array;
    }

    public int getArraySize() {
        return arraySize;
    }


    public void setArraySize(int size) {
        this.arraySize = size;
    }

    public void createArray() {
        array = new String[arraySize];
    }
}

设置新大小后,必须初始化数组变量。将您的方法更改为:

//Sets SID Array
public void setArraySize(int setArraySize) {
    //Array size int
    //checks array if array size is greater than 0
    if (setArraySize > 0) {
        //sets array size
        //SIDArray
        arraySize = setArraySize;
        SIDArray = new String[arraySize];
    }
}

问题是您正在实例化一个具有固定大小(0)的数组。那不一定不好。。。真正的问题是,在
setArraySize(int-setArraySize)
方法中,您更新了arraySize变量,但数组从未意识到此调用!!! 你应该使用类似于

SIDArray = new String[arraySize];

但即使这样做,调整数组大小也会破坏它以前的所有内容。。。请注意这一点。

谢谢您的回复,但我需要一个数组的访问器方法。您已经有了数组的访问器方法!正如我所建议的,仅更改setArraySize方法实现。问题是,我需要为数组提供一个访问器方法,如果在方法中初始化数组,则无法提供该方法。您可以为数组提供一个访问器方法,无论它在何处初始化。访问器方法如下所示:public String[]getArray(){return yourArray;}但是如果数组在'setArraySize'方法中,我如何获取它?为什么要从setArraySize方法返回数组?为什么要用这种方法来处理数组?这个类的范围很好。该方法应仅且仅将数组大小变量设置为其名称所描述的值