假设数组 foo 由交替值组成,索引为 0 = true、1 = false、2 = true 等。 例如:
for (int index = 0; index < foo.length; index++)
我正在尝试找到一种仅使用一条语句来完成此代码的方法。到目前为止我唯一的想法是:
foo[index] = !index--; // I want it to check the last Boolean of index, and reverse it
这当然是糟糕的代码并且不存在,但是有没有一种方法可以替代并使其看起来像这样:
foo[index] = (code that goes in here);
最佳答案
因为 boolean 数组最初都是 false。
boolean [] foo = new boolean[100];
for (int i = 0; i < foo.length; i+=2)
foo[i] = true;
关于Java:数组中交替 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38137089/