Java:自动装箱整数对象数组,从 LinkedList.toArray() 转换为 int
我想使用类似于以下内容的代码:
int letterIndex[];
LinkedList<Integer> letterList;
...
if(!letterList.isEmpty()) letterIndex = (Integer[])letterList.toArray();
但是,这是不允许的,并且显然在转换为 int[]Integer[]
代码>.如果不将 letterIndex
声明为 Integer[]
而不是 int[]
,我将如何完成等效操作?
I would like to use code similar to the following:
int letterIndex[];
LinkedList<Integer> letterList;
...
if(!letterList.isEmpty()) letterIndex = (Integer[])letterList.toArray();
However, it is not allowed, and apparently the cast to Integer[]
is not autoboxed when converting to int[]
. How would I accomplish the equivalent without declaring letterIndex
as Integer[]
instead of int[]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须创建一个新数组并分配
Integer[]
数组中的每个值。Apache commons-lang 有
ArrayUtils.toPrimitive(wrapperArray)
。You'd have to create a new array and assign each value from the
Integer[]
array.Apache commons-lang has
ArrayUtils.toPrimitive(wrapperArray)
.你为什么使用原语?
您可以将其更改为:
整数[]字母索引;
Why are you using primitives?
Can you change it to:
Integer[] letterIndex;