简体   繁体   English

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

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

This method is supposed to: 该方法应该:

  1. Poke through a text file filled with 32 rows of 10 hex strings. 在一个由32行的10个十六进制字符串填充的文本文件中进行戳戳。

  2. Break them apart using a whitespace in between each as delimiter. 使用它们之间的空格作为分隔符将它们分开。

  3. Store the hex strings in an array with 10 indices. 将十六进制字符串存储在具有10个索引的数组中。

  4. Since I'm simulating memory I then load the array into a simulated Register class. 由于我正在模拟内存,因此我将数组加载到模拟的Register类中。

I've gotten it to this step, but I'm having trouble seeing why this arrayOutOfBounds exception is occurring for the arrayString[] array. 我已经到了这一步,但是我很难理解为什么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();

}

Why are you splitting the str in the inner loop? 为什么在内部循环中拆分str The str value is not changing anywhere inside those for loops. str值在for循环内的任何位置均未更改。 Can you try moving it out of the for loops, and just after String str=s.nextLine(); 您是否可以尝试将它移出for循环,并String str=s.nextLine();String str=s.nextLine();

Also what is this.length ? 还有this.lengththis.length what if this.length-1 is greater than 10 (assuming str.split returns 10 hex strings, as per your initial description)? 如果this.length-1大于10怎么this.length-1 (假设str.split返回10个十六进制字符串,按照您的初始描述)?

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

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