Jason Chen
Jason Chen

Reputation: 322

Two-dimensional arrays Processing

If I'm creating a 2-d boolean array in Processing, I would use the code:

boolean[][] elemts = new boolean[500][500];

After I create this array, are all the values false, true, or null by default? If it isn't false, how do I use a for loop or for-each loop to set all the values to false?

Upvotes: 0

Views: 1124

Answers (2)

user3669962
user3669962

Reputation:

You can use the following code:

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

Make sure to put this in a function, like void setup(). This changes every element to false.

Upvotes: 0

Jose Gonzalez
Jose Gonzalez

Reputation: 1478

Use this code yo set all values to false.

for(int i=0; i<500; i++){
  for(int j=0; j<500; j++){
    elements[i][j] = false;
  }
}

Anyway they are all false for default, you can test this by:

    boolean[] test = new boolean[4];

    for(int i=0; i<4; i++){
      println(test[i]);
    }

If you test this you'll see that you get all false values, because they are false for default. Regards Jose

Upvotes: 1

Related Questions