Android/Java:用于阻止调用数组的循环

Android/Java:用于阻止调用数组的循环,java,android,button,for-loop,Java,Android,Button,For Loop,我正在制作一个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.t

我正在制作一个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);
                }
            });
        }
    }
public int j=1;
公共int i=1;
公共最终整数[][]按钮=新整数[][]{
{R.id.top_left_button,R.id.top_center_button,R.id.top_right_button},
{R.id.left_按钮,R.id.center_按钮,R.id.right_按钮},
{R.id.bottom_left_button,R.id.bottom_center_button,R.id.bottom_right_button};
私人按钮;
公共无效设置播放器(按钮按钮,整数x,整数y){
按钮。setText(“!”);
lastButton=(按钮)findViewById(按钮[x][y]);
lastButton.setText(“”);
lastButton=按钮;
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_游戏);
ctx=这个;
最终游戏引擎游戏=新游戏引擎();
lastButton=(按钮)findViewById(R.id.center_按钮);
对于(i=0;i
您需要问的问题是:单击按钮时,
i
j
的值是多少

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

这应该起作用:

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);
                }
            });
        }
    }
for(i=0;i

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

您需要问的问题是:什么t是单击按钮时
i
j
的值

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

这应该起作用:

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);
                }
            });
        }
    }
for(i=0;i

这将起作用的原因是,由于作用域,您在每次外部迭代中创建一个新的
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}};
for (int i = 0; i < buttons.length; i++) {
    for (int j = 0; j < buttons[i].length; j++) {
    ...
    ...
    }
}
在循环中,您可以初始化新的i和j变量,如下所示:

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}};
for (int i = 0; i < buttons.length; i++) {
    for (int j = 0; j < buttons[i].length; j++) {
    ...
    ...
    }
}
for(int i=0;i

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

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

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}};
for (int i = 0; i < buttons.length; i++) {
    for (int j = 0; j < buttons[i].length; j++) {
    ...
    ...
    }
}
在循环中,您可以初始化新的i和j变量,如下所示:

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}};
for (int i = 0; i < buttons.length; i++) {
    for (int j = 0; j < buttons[i].length; j++) {
    ...
    ...
    }
}
for(int i=0;i

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

尝试此公共最终int[][]按钮=新int[3][3]现在将id分配给数组。@sonukumar:这会有什么区别?@Rohit5k2查看McGee答案以了解更多详细信息。我已经阅读了该答案,但您的评论有所不同。当您正确分配值时,不必输入大小,然后尝试此公共最终整数[]按钮=新整数[3][3]现在将id分配给数组。@sonukumar:这会有什么区别?@Rohit5k2请查看McGee答案以了解更多详细信息。我已经阅读了该答案,但您的评论有所不同。当您正确分配值时,不必输入大小。很好!但是,在我看来,更好的解决方案是将I和j声明为局部变量,除非vai和j的lue是后来使用的,我非常怀疑这一点。然而,在我看来,更好的解决方案是将i和j声明为局部变量,除非i和j的值是后来使用的,我非常怀疑