2

I need to pass a primitive int array (int[]) via intent extras, and use them in an extended BaseAdapter class, so I need to convert int[] to Integer[]

How can I do this?

Maroun
  • 94,125
  • 30
  • 188
  • 241
mobilGelistirici
  • 449
  • 4
  • 7
  • 19
  • Array adapter does this for you. Also http://stackoverflow.com/questions/880581/how-to-convert-int-to-integer-in-java?rq=1 – Alec Teal Nov 13 '13 at 12:50
  • The best solution is to change your code so it doesn't need to do this in the first place. an Integer[] can use 7x more memory than an int[]. – Peter Lawrey Nov 13 '13 at 12:58
  • right now, there is so little members of arrays. So memory is not a big problem. And does BaseAdapter (for a Gallery) work with primitive arrays ? – mobilGelistirici Nov 13 '13 at 13:00
  • [Trove4j](http://mvnrepository.com/artifact/net.sf.trove4j/trove4j/3.0.3) also has wrappers for primitives. Too bad, there are not so many examples on the net. – Andrey Chaschev Nov 13 '13 at 13:50

2 Answers2

4

I am afraid theres no better solution than this

int[] a1 = ...
Integer[] a2 = new Integer[a1.length];
for(int i = 0; i < a1.length; i++)
   a2[i] = a1[i];
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

From @Eddie's answer here, you might want to look at this if you have access to the Apache lang library:

   Integer[] newArray = ArrayUtils.toObject(oldArray);
Community
  • 1
  • 1
Anudeep Bulla
  • 8,318
  • 4
  • 22
  • 29