Damian
Damian

Reputation: 55

Fill two-dimensional array with boolean value

In my class I have these properties:

boolean rendered[][] = new boolean[][]{};
String tabs[] = { "Tab 1", "Tab 2" };
int rows = 10;

... and I want to create an array with two main levels (two elements in tabs array), and each level would have 10 (variable rows) elements with false value.

Upvotes: 3

Views: 27889

Answers (3)

Christopher Francisco
Christopher Francisco

Reputation: 16278

First, you should tell the compiler how long is your array:

boolean rendered[][] = new Boolean[4][5];

Then you can proceed filling it

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

Upvotes: 1

You probably want Arrays#fill:

boolean rendered[][] = new boolean[rows][columns]; // you have to specify the size here
for(boolean row[]: rendered)
    Arrays.fill(row, false);

(Arrays#fill can only work on a one-dimensional array, so you'll have to iterate over the rest of the dimensions in a for loop yourself.)

Upvotes: 0

arynaq
arynaq

Reputation: 6870

You are free to think of it as [row][column] or [column][row] but the former has a history of usage.

int rows = 10, int columns = 2
boolean rendered[][] = new boolean[rows][columns];
java.util.Arrays.fill(rendered[0], false);
java.util.Arrays.fill(rendered[1], false);

Upvotes: 5

Related Questions