Is it possible to construct a Java stream expression to return a 2D boolean array, all values set to true?

huangapple go评论64阅读模式
英文:

Is it possible to construct a Java stream expression to return a 2D boolean array, all values set to true?

问题

我正在学习流表达式,并尝试使用它们构造一个二维布尔数组,其中所有值都设置为 true。类似于:

boolean[][] bool_array = [流表达式返回一个二维布尔数组 boolean[h][w]所有值都设置为 true]

是否可以实现这样的操作,表达式会是什么样的呢?

我知道可以使用流来创建 int[][] 数组,例如:

int h = 5;
int w = 8;
int[][] int_array = IntStream.range(0, h).mapToObj(i -> IntStream.range(0, w)
              .map(j -> 1).toArray()).toArray(int[][]::new);

可以得到填充了 1 的 int[5][8] 数组。但是尝试对 boolean[][] 做类似操作:

boolean[][] bool_array = IntStream.range(0, h).mapToObj(i -> IntStream.range(0, w)
              .mapToObj(j -> true).toArray()).toArray(boolean[][]::new);

会抛出 ArrayStoreException 异常。

英文:

I'm learning about stream expressions and trying to use them to construct a 2D boolean array, with all values set to true. Something like:

boolean[][] bool_array = [stream expression returning a
                             2D array boolean[h][w], all values set to true]

Is this possible to do and what would the expression be?

I know int[][] arrays may be created using streams, for example

int h=5;
int w=8;
int[][] int_array = IntStream.range(0,h).mapToObj(i->IntStream.range(0,w).
          map(j->1).toArray()).toArray(int[][]::new);

returns an int[5][8] filled with ones. But trying to get this to work for boolean[][]

boolean[][] bool_array = IntStream.range(0,h).mapToObj(i->IntStream.range(0,w).
          mapToObj(j->true).toArray()).toArray(boolean[][]::new);

throws an ArrayStoreException.

答案1

得分: 4

如果您想使用流来完成,可以按照以下方式进行:

int rows = 5;
int cols = 5;

boolean[][] bool2D = IntStream.range(0, rows)
		.mapToObj(r -> {
			boolean[] rr;
			Arrays.fill(rr = new boolean[cols], true);
			return rr;
		}).toArray(boolean[][]::new);

for (boolean[] b : bool2D) {
	System.out.println(Arrays.toString(b));
}

输出:

[true, true, true, true, true]
[true, true, true, true, true]
[true, true, true, true, true]
[true, true, true, true, true]
[true, true, true, true, true]
英文:

If you want to do it with streams you can do it like this:

int rows = 5;
int cols = 5;

boolean[][] bool2D = IntStream.range(0, rows)
		.mapToObj(r -> {
			boolean[] rr;
			Arrays.fill(rr = new boolean[cols], true);
			return rr;
		}).toArray(boolean[][]::new);


for (boolean[] b : bool2D) {
	System.out.println(Arrays.toString(b));
}

Prints

[true, true, true, true, true]
[true, true, true, true, true]
[true, true, true, true, true]
[true, true, true, true, true]
[true, true, true, true, true]


</details>



# 答案2
**得分**: 1

我发现了一种创建`Boolean[][]`数组(也就是对象类型的boolean,而不是原始类型)的方法。这一行与我在问题中的那行几乎相同,只是在第一个toArray()中加入了对`Boolean[]`的强制类型转换。

```java
Boolean[][] bool_array = IntStream.range(0, h)
    .mapToObj(i -> IntStream.range(0, w)
        .mapToObj(j -> true)
        .toArray(Boolean[]::new))
    .toArray(Boolean[][]::new);
英文:

I've discovered a way to make a Boolean[][] array (that is, the Object type boolean, not the primitive type). The line is almost the same as the one in my question, but with a cast to Boolean[] in the first toArray()

Boolean[][] bool_array = IntStream.range(0,h).mapToObj(i-&gt;IntStream.range(0,w).
           mapToObj(j-&gt;true).toArray(Boolean[]::new)).toArray(Boolean[][]::new);

答案3

得分: -5

更好地使用循环更为方便。Lambda语句和流并不能从根本上取代循环,在这种情况下,使用循环所带来的简单性要简单得多,处理起来也更容易。

需要注意的是,我的回答也基于学习关于lambda表达式。当它们刚出现时,我也认为它们可以做许多循环可以做的事情。然而,学习一种新的工具或方法也包括学会在何时使用它,一般的迭代绝对不是使用lambda合适的情况。

英文:

It's more expedient to use a loop. Lambda statements and streams do not fundamentally replace loops, and the simplicity you get with using loops in this context is far simpler and far easier to deal with.

Note that my answer is also based in learning about lambda expressions. When they first came out, I too thought they could do a lot of things that loops could. However, learning a new tool or approach also includes learning when not to use it, and general iteration is definitely not a circumstance when lambdas are fit for purpose.

huangapple
  • 本文由 发表于 2020年10月10日 04:41:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64287048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定