简体   繁体   English

Java颠倒数组

[英]Java upside down array

I was trying to make a simple TicTacToe game. 我试图制作一个简单的TicTacToe游戏。

Here is the code for the coordination: 这是协调的代码:

for(int i=0;i<3;i++){
    for(int j=0;j<3;j++){
        buttons[i][j] = new XOButton();
        // if((i + j) % 2 != 0){
            buttons[i][j].setText(i + "," +j);
        // }
        p.add(buttons[i][j]);
    }
}

Currently, the coordination is from top the bottom, like this: 当前,协调是从上至下进行的,如下所示:

0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2

However, I want it to be from bottom to top like this: 但是,我希望它像这样从底部到顶部:

2,0 2,1 2,2
1,0 1,1 1,2
0,0 0,1 0,2

I tried to fix it by changing the value of i and j in the loop like this: 我试图通过在循环中更改i和j的值来解决此问题,如下所示:

for(int i=3;i>0;i--){
    for(int j=3;j>0;j--){
        buttons[i][j] = new XOButton();
        // if((i + j) % 2 != 0){
            buttons[i][j].setText(i + "," +j);
        // }
        p.add(buttons[i][j]);
    }
}

However, It did not work. 但是,它没有用。 It says 它说

Exception in thread 'main' java.lang.ArrayIndexOutOfBoundsException:

I tried to think of the cause of this problem, is it because I have to add date into arrays before the array[3][3]? 我试图考虑导致此问题的原因,是因为我必须在array [3] [3]之前在数组中添加日期吗? for example, array[0][0] ~ array[2][3] all have to be filled with data first? 例如,必须先用数据填充array [0] [0]〜array [2] [3]?

Any help would be appreciated. 任何帮助,将不胜感激。

You are using indices outside the bouds of the array. 您正在数组数组之外使用索引。 Array's indexes in java go from zero to length of the array minus one. Java中数组的索引从零到数组的长度减去一。

To avoid this kind iof problems you could loop on the size of the array buttons.length-1 and buttons[i].length-1 为了避免此类问题,您可以循环使用button.length-1buttons [i] .length-1数组的大小

for(int i=buttons.length-1;i>=0;i--){
    for(int j=buttons[i].length;j>=0;j--){
        buttons[i][j] = new XOButton();
        // if((i + j) % 2 != 0){
            buttons[i][j].setText(i + "," +j);
        // }
        p.add(buttons[i][j]);
    }
}
for(int i=7;i>=0;i--){
   for(int j=0;j<8;j++){
       buttons[i][j] = new XOButton();
       // if((i + j) % 2 != 0){
           buttons[i][j].setText(i + "," +j);
       // }
       p.add(buttons[i][j]);
    } 
}

The obvious answer seems to be that you start at i=8 instead of i=2. 显而易见的答案似乎是您从i = 8而不是i = 2开始。 This means that on the first pass of the loops i=8 and j=8 and buttons[8][8] is obviously out of bounds. 这意味着在循环的第一遍,i = 8和j = 8以及button [8] [8]显然超出范围。

Also, your break condition should be i>=0 or else i will never be equal to 0 (and the index of your arrays are from 0 to 2). 另外,您的中断条件应为i> = 0,否则我将永远不等于0(并且数组的索引从0到2)。

You are probably better off leaving your iterators i and j the way they were, and doing some math when setting the text of your coordinates. 您最好保留迭代器ij的状态,并在设置坐标文本时进行一些数学运算。 The way arrays work in Java, there is not a whole lot you can do to actually restructure the way they are stored, but you can certainly structure your mental image of how it is stored. 数组在Java中的工作方式,您无法做很多事情来真正地重组它们的存储方式,但是您可以肯定地构造自己关于存储方式的思想形象。

for(int i=0;i<3;i++){
    for(int j=0;j<3;j++){
        buttons[i][j] = new XOButton();
        // if((i + j) % 2 != 0){
            buttons[i][j].setText((buttons.length - i) + "," + (buttons[i].length - j));
        // }
        p.add(buttons[i][j]);
    }
}

That should get your arrays set up so they have the text laid out in the fashion you describe. 这样就可以建立您的数组,以便它们按照您描述的方式布置文本。 Notice I used (buttons.length - i) and (buttons[i].length - j) to "switch" the coordinates to count down. 注意,我使用(buttons.length - i)(buttons[i].length - j) “切换”坐标以倒数。 This is just a trick to avoid using undefined constants in the code, but you could technically replace both with 3 . 这只是避免在代码中使用未定义的常量的一种技巧,但是您可以在技术上将两者都替换为3

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

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