Android / Java:防止调用数组的 for 循环



我正在制作一个 3x3 网格类型的应用程序,我想通过添加 for 循环来调用按下以更改它的按钮。但是,我得到异常"java.lang.ArrayIndexOutOfBoundsException: length=3;index=3",因为 for 循环很奇怪。谁能为我弄清楚这一点?我是Java和编程的新手。

法典:

public int j = 1;
public int i = 1;
public final int[][] buttons = new int[][] {
        {R.id.top_left_button, R.id.top_center_button, R.id.top_right_button},
        {R.id.left_button, R.id.center_button, R.id.right_button},
        {R.id.bottom_left_button, R.id.bottom_center_button, R.id.bottom_right_button}};
private Button lastButton;
public void setPlayer(Button button, int x, int y){
    button.setText("!");
    lastButton = (Button)findViewById(buttons[x][y]);
    lastButton.setText(" ");
    lastButton = button;
}
@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    ctx = this;
    final GameEngine game = new GameEngine();
    lastButton = (Button)findViewById(R.id.center_button);
    for (i = 0; i < buttons.length; i++) {
        for (j = 0; j < buttons[i].length; j++) {
            final Button button = (Button)findViewById(buttons[i][j]);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Button b = (Button)v;
                    int x = i;
                    int y = j;
                    setPlayer(b, x , y);
                }
            });
        }
    }
您需要

问的问题是:单击按钮时ij的值是多少?

i的答案是:buttons.length.因为这是你离开循环时留下i的值。

这应该有效:

for (i = 0; i < buttons.length; i++) {
        final int iCopy = i;  
        for (j = 0; j < buttons[i].length; j++) {
            final Button button = (Button)findViewById(buttons[i][j]);
            final int jCopy = j;
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Button b = (Button)v;
                    int x = iCopy;
                    int y = jCopy;
                    setPlayer(b, x , y);
                }
            });
        }
    }

这样做的原因是,由于作用域,您将在每次外部迭代中创建一个新的iCopy实例,并在每次内部迭代中创建一个新的jCopy实例。每个匿名OnClickListener实现现在都将使用适当的副本。

你应该像这样声明和初始化你的数组:

public final int[][] buttons = {
        {R.id.top_left_button, R.id.top_center_button, R.id.top_right_button},
        {R.id.left_button, R.id.center_button, R.id.right_button},
        {R.id.bottom_left_button, R.id.bottom_center_button, R.id.bottom_right_button}};

在你的循环中,你可以像这样启动新的 i 和 j 变量:

for (int i = 0; i < buttons.length; i++) {
    for (int j = 0; j < buttons[i].length; j++) {
    ...
    ...
    }
}

无需在活动的顶部声明它们。

最新更新