当在类中定义数组大小时,为什么我会得到ArrayIndexOutOfBounds异常



对于我的项目,用户必须定义数组的大小,为此我创建了一个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.Arrays;导入java.util.regex.Pattern;

/****@作者Colby Leclerc*/公共类SIDArrayTest{

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

}

当我为数组创建了一个类变量时,我真的很困惑如何设置数组大小,同时让数组成为一个"get"-able方法。

在调用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];

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

最新更新