Java- 无法弄清楚为什么在我的缓存模拟中出现 arrayOutOfBoundsException
Java- Having trouble figuring out why there's an arrayOutOfBoundsException going on in my cache simulation
此方法应该:
浏览一个包含 32 行 10 个十六进制字符串的文本文件。
使用空格作为分隔符将它们分开。
将十六进制字符串存储在具有 10 个索引的数组中。
因为我在模拟内存,所以我将数组加载到模拟寄存器中 class。
我已经完成了这一步,但我无法理解为什么 arrayString[] 数组会出现此 arrayOutOfBounds 异常。
public void setRegister(Register[] r) throws FileNotFoundException{ //pokes through the
//file and extracts hexes
Scanner s = new Scanner("/Users/adpitt/Documents/document.txt"); //macOS path
Register reggie = new Register(); //holding reg
while (s.hasNextLine()) {
String str = s.nextLine(); //slam a line into the string
for(int i = 0; i <= mainMemSize-1; i++){ //iterate through array of registers
for(int j = 0; j <= this.length-1; j++){
String arrayString[] = str.split("\s+");//smash them to
//pieces, space = delimiter
reggie.reg[j] = arrayString[j];//THIS IS WHERE THE EXCEPTION OCCURS.
//I believe it's in arrayString[j],
//not sure why.
}//complete reggie!
r[i] = reggie;//load reggie into the register array to complete mm
}
}
s.close();
}
为什么要在内部循环中拆分 str
? str
值在那些 for 循环内的任何地方都没有改变。你能试着把它移出 for 循环,就在 String str=s.nextLine();
之后吗
还有什么是this.length
?如果 this.length-1
大于 10(假设 str.split returns 10 个十六进制字符串,根据您的初始描述)怎么办?
此方法应该:
浏览一个包含 32 行 10 个十六进制字符串的文本文件。
使用空格作为分隔符将它们分开。
将十六进制字符串存储在具有 10 个索引的数组中。
因为我在模拟内存,所以我将数组加载到模拟寄存器中 class。
我已经完成了这一步,但我无法理解为什么 arrayString[] 数组会出现此 arrayOutOfBounds 异常。
public void setRegister(Register[] r) throws FileNotFoundException{ //pokes through the
//file and extracts hexes
Scanner s = new Scanner("/Users/adpitt/Documents/document.txt"); //macOS path
Register reggie = new Register(); //holding reg
while (s.hasNextLine()) {
String str = s.nextLine(); //slam a line into the string
for(int i = 0; i <= mainMemSize-1; i++){ //iterate through array of registers
for(int j = 0; j <= this.length-1; j++){
String arrayString[] = str.split("\s+");//smash them to
//pieces, space = delimiter
reggie.reg[j] = arrayString[j];//THIS IS WHERE THE EXCEPTION OCCURS.
//I believe it's in arrayString[j],
//not sure why.
}//complete reggie!
r[i] = reggie;//load reggie into the register array to complete mm
}
}
s.close();
}
为什么要在内部循环中拆分 str
? str
值在那些 for 循环内的任何地方都没有改变。你能试着把它移出 for 循环,就在 String str=s.nextLine();
还有什么是this.length
?如果 this.length-1
大于 10(假设 str.split returns 10 个十六进制字符串,根据您的初始描述)怎么办?