我正在尝试使用Java 8中的新Stream API生成随机整数数组,但我还没有完全理解这个API。所以我需要帮助。以下是我的代码。
我正在尝试使用Java 8中的新Stream API生成随机整数数组,但我还没有完全理解这个API。所以我需要帮助。以下是我的代码。
Random random = new Random();
IntStream intStream = random.ints(low, high);
int[] array = intStream.limit(limit) // Limit amount of elements
.boxed() // cast to Integer
.toArray();
但是这段代码返回了对象数组。它有什么问题吗?
boxed()
。这就是你整个问题的关键所在。 - Louis WassermanString
。如果它应该将每个随机的int
解释为代码点,请将此答案的int
流与“[将IntStream
打印为String
的最简单方法](https://dev59.com/UGIj5IYBdhLWcg3wb0rX#20268845)”结合起来。 - Holger