Java : autoboxing of an Object array of integers, cast to int from LinkedList.toArray()

StackOverflow https://stackoverflow.com/questions/5412056

  •  29-10-2019
  •  | 
  •  

문제

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[]?

도움이 되었습니까?

해결책

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;

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top