0

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

TurtleToes
  • 2,047
  • 2
  • 17
  • 17
  • final int s=list.size();int[] r=new int[s];for(int i=0;i – bestsss Mar 23 '11 at 22:41
  • Obviously... but I was looking for something provided by the class library. – TurtleToes Mar 24 '11 at 04:30
  • it's not possible to take a `java.util.Collection` and transform it for a primitive array. To put it simply: the generic type doesn't exist during runtime (and actually practically doesn't exist on compile time). To do that you'd need some method w/ a signature like that toArray(Collection extends Number> c, Object array) and the array should be of a primitive type. The code cannot be even optimized properly and to work efficiently the JVM would need to expend some very special care and all that for a single line of code... – bestsss Mar 24 '11 at 19:50

2 Answers2

2

You'd have to create a new array and assign each value from the Integer[] array.

Apache commons-lang has ArrayUtils.toPrimitive(wrapperArray).

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • so basically it would be more straightfoward to use an ArrayList instead? I just hate using them since they seem so inefficient. – TurtleToes Mar 23 '11 at 21:54
  • @TurtleToes well, it depends. There is no 'obviously more efficient way'. But I think that's microoptimization in most cases. – Bozho Mar 23 '11 at 21:57
  • I suppose I will just create the integer array dynamically by the size of the list and step thru it to convert each index. Thanks for the help. – TurtleToes Mar 23 '11 at 22:00
  • @Bozho, actually it's hard to imagine such tasks make it to a popular library. – bestsss Mar 23 '11 at 22:43
  • @bestsss - I didn't get what you mean :) – Bozho Mar 23 '11 at 22:49
  • @Bozho, I mean converting a collection to a primitive array. It's a single liner (more or less) and if you have to do it, something has gone wrong probably. Boxing primitives is more or less inefficient but well, ok. Converting them (the boxed ones)back to a primitive array usually it's an uncommon task to me. – bestsss Mar 23 '11 at 22:53
  • @bestsss when some APIs are incompatible it becomes necessary. – Bozho Mar 23 '11 at 23:01
0

Why are you using primitives?

Can you change it to:

Integer[] letterIndex;

Dave L.
  • 9,595
  • 7
  • 43
  • 69
  • he was explicit he does not want that. – Bozho Mar 23 '11 at 22:01
  • You are right. In a real-world application I probably would use an Integer[] array. However, I am fairly new to Java and I'm probing the extents of what I can do and how to do it. – TurtleToes Mar 23 '11 at 22:01
  • *In a real-world application I probably would use an Integer[] array.* That's a bad decision – bestsss Mar 23 '11 at 22:42
  • In the class I was designing, the values were declared final, so an Integer[] array would have served just as well. The code to create the array was in a constructor. However, either way, the class is pointless, and is just me poking my away around the language. – TurtleToes Mar 23 '11 at 22:54