我想要一个包含1到500范围内整数的列表.是否有一些方法可以使用Guava(或简单的Java)创建此列表,而无需遍历范围并在我自己的范围内单独添加值码?
解决方案如下(在这里发布,因为一些答案提供了正确的信息,但没有一个给出完整的解决方案):
__PRE__
结果是类型 __CODE__
在使用连续的短裤范围填充列表的基础上,我尝试生成一组原始短裤.事实证明这比预期的要难得多.
Short[] range = IntStream.range(0, 500).mapToObj(value -> (short) value).toArray(Short[]::new) 工作但是:
short[] range = IntStream.range(0, 500).mapToObj(value -> (short) value).toArray(short[]::new) 生成编译器错误:
method toArray in interface Stream<T> cannot be applied to given types;
required: IntFunction<A[]>
found: short[]::new
reason: inference variable A has incompatible bounds
equality constraints: short
upper bounds: Object
where A,T are type-variables:
A extends Object declared in method <A>toArray(IntFunction<A[]>)
T extends Object declared in interface Stream
Run Code Online (Sandbox Code Playgroud)
这似乎是两个问题的交集:
shorts 的实现.有任何想法吗?