3

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.

3 Answers 3

5

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);
Sign up to request clarification or add additional context in comments.

3 Comments

boolean (not Boolean) arrays are by default filled with false. Last two lines are only required if we want to reset already used array, or set values to true.
The more you know, I learned something from answering a question ;)
How do you declare that type of Array as a class field?
1

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;

1 Comment

If you use boolean rendered[][] = new boolean[4][5]; you won't need to iterate over entire array because every element for boolean array is by default set to false.
0

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.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.