Java:数组中交替 boolean 值

标签 java arrays

假设数组 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/

相关文章:

javascript - 在 Javascript 中使用 PHP 数组获取 Javascript 变量作为索引

java - 使用 RandomAccessFile 编写学生数组时遇到问题

php - 我如何使用数组中的值? (php)

javascript - 如果数组中有假值则返回

java - 根据参数在java中舍入 double 值

java - 如何在录制时减少视频大小,Android?

java - xgettext 提取了错误的文字

java - 反射获取的方法的执行时间是否更长?

java - 在 MySQL 中创建 VIEW 期间出现语法错误

javascript - 如何在 JavaScript 中有效地比较一个对象数组与另一个数组?