繁体   English   中英

Java-无法弄清为什么在缓存模拟中会发生arrayOutOfBoundsException

[英]Java- Having trouble figuring out why there's an arrayOutOfBoundsException going on in my cache simulation

该方法应该:

  1. 在一个由32行的10个十六进制字符串填充的文本文件中进行戳戳。

  2. 使用它们之间的空格作为分隔符将它们分开。

  3. 将十六进制字符串存储在具有10个索引的数组中。

  4. 由于我正在模拟内存,因此我将数组加载到模拟的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.lengththis.length 如果this.length-1大于10怎么this.length-1 (假设str.split返回10个十六进制字符串,按照您的初始描述)?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM