Java Android Studio For循环超出其限制

Java Android Studio For循环超出其限制,java,android,for-loop,Java,Android,For Loop,当我在Android设备上运行下面的代码时,我得到一个ArrayOutOfBounds异常,表示y=6。然而,这在理论上是不可能的,因为for循环的边界是y

当我在Android设备上运行下面的代码时,我得到一个ArrayOutOfBounds异常,表示y=6。然而,这在理论上是不可能的,因为for循环的边界是y<6。我寻找了可能的解释,但没有找到。matrix1Numbers的大小为6乘6,每个维度保持0-5

for (z = 0; z < 6; ++z) {
        for (y = 0; y < 6; ++y) {
            matrix1Numbers[z][y].addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(  CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (matrix1Numbers[z][y].getText().toString().equals("")) {
                        for (int a = 0; a < z; ++a) {
                            for (int b = y; b < 6; ++b) {
                                matrix1Numbers[a][b].setText("");
                                matrix1Numbers[a][b].setBackgroundColor(Color.WHITE);
                            }
                        }

                        for (int a = z; a < 6; ++a) {
                            for (int b = 0; b < y; ++b) {
                                matrix1Numbers[a][b].setText("");
                                matrix1Numbers[a][b].setBackgroundColor(Color.WHITE);
                            }
                        }

                        for (int a = z; a < 6; ++a) {
                            for (int b = y; b < 6; ++b) {
                                matrix1Numbers[a][b].setText("");
                                matrix1Numbers[a][b].setBackgroundColor(Color.WHITE);
                            }
                        }
                    } else {
                        if (z > matrix1MaxX) {
                            matrix1MaxX = z;
                        }
                        if (y > matrix1MaxY) {
                            matrix1MaxY = y;
                        }

                        for (int a = 0; a < matrix1MaxX; ++a) {
                            for (int b = 0; b < matrix1MaxY; ++b) {
                                matrix1Numbers[a][b].setBackgroundColor(Color.BLUE);
                            }
                        }
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });


            matrix2Numbers[z][y].addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (matrix2Numbers[z][y].getText().toString().equals("")) {
                        for (int a = 0; a < z; ++a) {
                            for (int b = y; b < 6; ++b) {
                                matrix2Numbers[a][b].setText("");
                                matrix2Numbers[a][b].setBackgroundColor(Color.WHITE);
                            }
                        }

                        for (int a = z; a < 6; ++a) {
                            for (int b = 0; b < y; ++b) {
                                matrix2Numbers[a][b].setText("");
                                matrix2Numbers[a][b].setBackgroundColor(Color.WHITE);
                            }
                        }

                        for (int a = z; a < 6; ++a) {
                            for (int b = y; b < 6; ++b) {
                                matrix2Numbers[a][b].setText("");
                                matrix2Numbers[a][b].setBackgroundColor(Color.WHITE);
                            }
                        }
                    } else {
                        if (z > matrix2MaxX) {
                            matrix2MaxX = z;
                        }
                        if (y > matrix2MaxY) {
                            matrix2MaxY = y;
                        }

                        for (int a = 0; a < matrix2MaxX; ++a) {
                            for (int b = 0; b < matrix2MaxY; ++b) {
                                matrix2Numbers[a][b].setBackgroundColor(Color.BLUE);
                            }
                        }
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });



        }
(z=0;z<6;++z)的
{
对于(y=0;y<6;++y){
matrix1Numbers[z][y].addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
if(matrix1Numbers[z][y].getText().toString().equals(“”){
对于(int a=0;amatrix1MaxX){
matrix1MaxX=z;
}
如果(y>matrix1MaxY){
matrix1MaxY=y;
}
对于(int a=0;amatrix2MaxX){
矩阵x2maxx=z;
}
如果(y>matrix2MaxY){
matrix2MaxY=y;
}
对于(int a=0;a
检查代码的其他部分是否与您的
y
z
一起工作,因为您将它们声明在循环之外。可能有一些代码正在修改它们,而您不知道副作用。

如何存储
y
z
?我的意思是..它是静态的还是其他的?@Rothens,您完全是这样的是的,我检查并移除了我的comment@darkterbears,请告诉我们异常发生的位置(在哪一行)第11行,第一个if语句。Debug说y的值是6,限制是6。