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
  • 91,013
  • 29
  • 181
  • 233
mobilGelistirici
  • 449
  • 3
  • 7
  • 18
  • 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

3 Answers3

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
  • 129,181
  • 28
  • 195
  • 266
0

See following code:

int[] old = ....    
Integer[] arr = new Integer[old.length];
System.arraycopy(old, 0, arr, 0, old.length);

Or you can use Apache lang library, then you can use the ArrayUtils.toObject(int[]) method like this:

Integer[] newArray = ArrayUtils.toObject(oldArray);
Sumit Singh
  • 24,095
  • 8
  • 74
  • 100
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
  • 7,830
  • 3
  • 21
  • 29