当我在我的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) {
}
});
}
答案 0 :(得分:1)
检查代码的其他部分代码是否适用于您的y
和z
,因为您在循环之外声明它们。可能有一些代码可以修改它们,而你却不知道副作用。