[英]Java- Having trouble figuring out why there's an arrayOutOfBoundsException going on in my cache simulation
該方法應該:
在一個由32行的10個十六進制字符串填充的文本文件中進行戳戳。
使用它們之間的空格作為分隔符將它們分開。
將十六進制字符串存儲在具有10個索引的數組中。
由於我正在模擬內存,因此我將數組加載到模擬的Register類中。
我已經到了這一步,但是我很難理解為什么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();
在String str=s.nextLine();
還有this.length
是this.length
? 如果this.length-1
大於10怎么this.length-1
(假設str.split返回10個十六進制字符串,按照您的初始描述)?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.