简体   繁体   English

用布尔值填充二维数组

[英]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. ...我想创建一个包含两个主要级别的数组( tabs数组中有两个元素),每个级别都有10个(可变rows )元素,其false

You are free to think of it as [row][column] or [column][row] but the former has a history of usage. 您可以将其视为[row] [column]或[column] [row],但前者具有使用历史。

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

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;

You probably want Arrays#fill : 你可能想要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.) Arrays#fill只能在一维数组上工作,所以你必须自己迭代for循环中的其余维度。)

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

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