简体   繁体   English

Java中2D锯齿状阵列上的边界异常

[英]Out of Bounds Exception on a 2D Ragged Array in Java

Problem solved, I ended up need a seperate counter for the array position. 问题解决了,我最终需要为阵列位置使用单独的计数器。 Thanks for the help! 谢谢您的帮助! I'm writing a small app that takes a string, processes each string into 7-bits of binary code and then fills in a musical scale based on the string. 我正在编写一个小型应用程序,该应用程序需要一个字符串,将每个字符串处理为7位二进制代码,然后根据该字符串填充音阶。 For instance, if I had the binary 1000100, in the key of C Major that would give me the notes C and G(C 0 0 0 G 0 0). 例如,如果我有二进制文件1000100,则在C Major的键中会给我音符C和G(C 0 0 0 G 0 0)。

I'm having an issue with a specific piece of code that takes an input of String[] (in which each element is a single character worth of binary, 7-bits) and processes each individual character in the strings themselves and stores the index number of where 1's occur in the string. 我遇到了一段特定的代码问题,该代码采用String []输入(其中每个元素是一个单独的字符,值二进制7位),然后处理字符串中的每个字符并存储索引字符串中出现1的数目。 For example, the string 1000100 would output 1 and 5. 例如,字符串1000100将输出1和5。

Here's the method that does that: 这是执行此操作的方法:

public static String[][] convertToScale(String[] e){
    String[][] notes = new String[e.length][]; //create array to hold arrays of Strings that represent notes
    for(int i = 0; i < e.length; i++){
        notes[i] = new String[findOccurancesOf(e[i])]; //create arrays to hold array of strings
        for(int x = 0; x < e[i].length(); x++){ 
            if((e[i].charAt(x)) != 48){ //checks to see if the char being evaluated is 0(Ascii code 48)
                notes[i][x] = Integer.toString(x + 1); // if the value isn't 0, it fills in the array for that position.the value at x+1 represents the position of the scale the note is at
            }
        }
    }
    return notes;
}

Here is the code that is uses to get the occurrences of 1 in e[1]: 这是用于获取e [1]中1出现的代码:

public static int findOccurancesOf(String s){
    int counter = 0;
    for(int i = 0; i < s.length(); i++ ) {
        if( s.charAt(i) == 1 ) {
            counter++;
        } 
    }
    return counter;
}

The issue I'm having is with the convertToScale method. 我遇到的问题是convertToScale方法。 When using "Hello world" as my input(the input gets converted into 7-bit binary before it gets processed by either of these methods) it passes through the 2nd for-each loop just fine the first time around, but after it tries to fill another spot in the array, it throws 当使用“ Hello world”作为我的输入时(在使用任何一种方法对输入进行处理之前,输入都已转换为7位二进制数),它会第一次通过第二个for-each循环,但是在尝试填充数组中的另一个点,它会抛出

java.lang.ArrayIndexOutOfBoundsException: 3 java.lang.ArrayIndexOutOfBoundsException:3

EDIT:It occurs in the line notes[i][x] = Integer.toString(x + 1); 编辑:它出现在行notes[i][x] = Integer.toString(x + 1); of the convertToScale method. convertToScale方法。 I've run the debugger multiple times through after trying the proposes changes below and I still get the same error at the same line. 在尝试了下面的提议更改之后,我已经多次运行调试器,但在同一行仍然遇到相同的错误。 The findOccurancesOf method returns the right value(When evaluating H(1001000) it returns 2.) So the thing that confuses me is that the out of bounds exception comes up right when it fills the 2nd spot in the array. findOccurancesOf方法返回正确的值(当评估H(1001000)时,它返回2。)所以令我感到困惑的是,超出范围异常在填充数组的第二个位置时就出现了。

Also, feel free to tell me if anything else is crazy or my syntax is bad. 另外,请随时告诉我是否还有其他疯狂的事情或我的语法不好。 Thanks! 谢谢!

In findOccurancesOf() : findOccurancesOf()

if( s.charAt(i) == 1 ) { should be if( s.charAt(i) == '1' ) { to check for the character '1'. if( s.charAt(i) == 1 ) {应该是if( s.charAt(i) == '1' ) {检查字符'1'。

Otherwise it's looking for the character with ASCII value 1. 否则,它将寻找ASCII值为1的字符。

There is an out of bounds exception because if findOccuranceOf() returns the wrong value, then notes[i] is not constructed with the correct length in the following line of convertToScale() : 存在一个超出范围的异常,因为如果findOccuranceOf()返回错误的值,那么在convertToScale()的以下行中, notes[i]长度不正确:

notes[i] = new String[findOccurancesOf(e[i])];

In addition, you probably want to use something like: 另外,您可能想要使用类似以下的内容:

notes[i][c++] = Integer.toString(x + 1);

with some counter c initialized to 0, if I understand your intentions correctly. 如果我正确理解了您的意图,则将一些计数器c初始化为0。

The reason for AIOOBE lies in this line: AIOOBE的原因在于:

notes[i] = new String[findOccurancesOf(e[i])]; //create arrays to hold array of strings

Where you call findOccurancesOf method to find occurance of 1 in your String say Hello which you dont find and return 0 and then you call notes[i][x] = Integer.toString(x + 1); 在调用findOccurancesOf方法以查找字符串中出现1的位置时,请说您好,您找不到并返回0,然后调用notes[i][x] = Integer.toString(x + 1); with x as 0. Now since you never allocated space, you get array index out of bound exception. x为0。由于您从未分配空间,因此数组绑定超出了异常范围。

I would suggest the folowing: 我建议以下:

  • Validate your string before assigning the index say to be greater than 0 or something. 在分配索引值大于0或其他值之前,请先验证您的字符串。
  • Initialize you notes[i] as notes[i] = new String[e[i].length]; 将notes [i]初始化为notes[i] = new String[e[i].length];
  • Checking character with single quotes like a == '1' rather than a == 1 用单引号检查字符,例如a == '1'而不是a == 1

The exception is caused by what almas mentioned, note however, that your logical error is most likely inside findOccurencesOf method, if the idea was to find all the '1' chars inside a string you must change to what I outlined below, note the apostrohes. 异常是由提到的almas引起的,但是请注意,您的逻辑错误最有可能在findOccurencesOf方法内,如果要在字符串中查找所有'1'字符,则必须更改为我在下面概述的内容,注意撇号。 Otherwise a char is getting converted to a byte ascii code, and unless matched with a code of ascii code one, the method will return 0, causing your exception 否则,char将被转换为字节ASCII代码,并且除非与ASCII代码1匹配,否则该方法将返回0,从而导致异常

 if( s.charAt(i) == '1' ) {

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

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