简体   繁体   English

带程序的嵌套循环,布尔数组?

[英]Nested Loop with program, boolean arrays?

In the constructor of the model class, I need to allocate the memory of this array of booleans (boolean[ ][ ] is_hidden;). 在模型类的构造函数中,我需要分配此布尔数组(boolean [] [] is_hidden;)的内存。 I also need to set them to true, but have no idea how this happens, a nested loop will have to be used like the one in the paint method at he bottom, in order to set each element. 我还需要将它们设置为true,但是不知道如何发生,必须设置嵌套循环,就像在底部的paint方法中那样,以设置每个元素。

class MineFinderModel {
public static int MINE_SQUARE = 10;
public static int EMPTY_SQUARE = 0;

int num_of_cols;
int num_of_rows;
int[][] the_minefield;
boolean[][] is_hidden;

public MineFinderModel(int n_cols, int n_rows) {
    num_of_rows = n_rows;
    num_of_cols = n_cols;
    the_minefield = new int[num_of_cols][num_of_rows];
    is_hidden = new boolean[][];
}

Paint method example: 绘制方法示例:

                   for (int i = 0;i<numCols;i++)
         {
        for(int j = 0;j<numRows;j++)
        {
            Rectangle r = getRect(i,j);
            g.setColor(Color.black);
            g2.draw(r);

            if(i==0&&j==0)                                  {
                g2.drawOval(x,y,w,h);
            g2.fillOval(x,y,w,h);
            }
            if(i==0&&j==(numRows-1))
            g2.fillOval(x,y,w,h);

            if(i==(numCols-1)&&j==0)
            g2.fillOval(x,y,w,h);

            if(i==(numCols-1)&&j==(numRows-1))
            g2.fillOval(x,y,w,h);

You need to define the array with the sizes eg 您需要使用大小定义数组,例如

is_hidden = new boolean[cols][rows]();

and iterate through, setting each cell to true (booleans, and boolean arrays, default to false ). 并进行遍历,将每个单元格设置为true (布尔值和布尔数组,默认为false )。

Note that Arrays.fill() exists, but will only get you halfway, since it won't fill multidimensional arrays. 请注意,存在Arrays.fill() ,但只会使您半途而废,因为它不会填充多维数组。 You can use this, but you'd have to iterate through the rows, and use Arrays.fill on each row. 您可以使用它,但是您必须遍历各行,并在每行上使用Arrays.fill Perhaps not worthwhile in this example, but worth being aware of regardless. 在此示例中,也许不值得,但无论如何都应引起注意。

Try this: 尝试这个:

    int num_of_cols = 2;
    int num_of_rows = 3;
    boolean[][] is_hidden;
    is_hidden = new boolean [num_of_cols][num_of_rows];

    for (int i = 0; i < num_of_cols; i++) {
        for (int j = 0; j < num_of_rows; j++) {
            is_hidden[i][j] = true;
        }
    }

You can see it is now correctly initialized: 您可以看到它现在已正确初始化:

    for (boolean[] col : is_hidden) {
        for (boolean elem  : col) {
            System.out.println(elem);
        }
    }

when you define a boolean array the value of all the elements are false by default. 定义布尔数组时,默认情况下所有元素的值均为false。 I would suggest instead of looping through all the elements, implement your conditions in way that you can use the default false value. 我建议不要循环遍历所有元素,而是以可以使用默认false值的方式实现条件。

Eg. 例如。

boolean[][] isEnabled =  new boolean[10][10];
// code to set all elements to true
if(isEnabled[i][j]){
    //some code
}

this can be easily replaced by 这可以很容易地被替换

boolean[][] isDisabled =  new boolean[10][10];
if(! isDisabled[i][j]){
    //some code
}

You can save processing time this way and code looks neat :). 您可以通过这种方式节省处理时间,并且代码看起来很整洁:)。

Simply write a nested loop. 只需编写一个嵌套循环。

for (int i = 0;i<n_rows;i++){
    for(int j = 0;j<n_cols;j++){
        is_hidden[n_rows][n_cols] = true;
    }
}

我为您提供了一个简单的解决方案:与其使用数组is_hidden而不是用true填充它的每个元素,而是使用名称isVisible,并且根本不填充它,因为它的每个元素都已经初始化为false;)

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

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