int can be auto-boxed into Integer in Tiger, but I want to convert
int[] to Integer[]. How to do that? Thanks.
Thomas Hawtin - 08 May 2006 13:28 GMT
> int can be auto-boxed into Integer in Tiger, but I want to convert
> int[] to Integer[]. How to do that? Thanks.
static Integer[] box(int[] array) {
int num = array.length;
Integer[] boxed = new Integer[num];
for (int ct=0; ct<num; ++ct) {
boxed[ct] = Integer.valueOf(array[ct]);
}
return boxed;
}
Of course, changes made to the new array will not be updated in the old one.
To box arrays automatically would be even more confusing than
autoboxing. Consider:
Integer[] boxed = array;
boxed[0] = -1;
array[0] = 42;
System.out.println(boxed[0]);
If we assume autoboxing of arrays and array is an int[], then the code
will print -1. If array were an Integer[], then the code would print 42.
Alternatively, I guess you could produce (or find) a List<Integer>
implementation that backs to an int[] (a little like
java.util.Arrays.asList).
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
lionchao@gmail.com - 08 May 2006 14:26 GMT
List<Integer> list=new Vector<Integer>();
list.add(1);
list.add(2);
then I want to change the list to int[], but only list.toArray(new
Integer[0]) works which gets an Integer[].
--
Eric Chao
VisionSet - 08 May 2006 14:54 GMT
> List<Integer> list=new Vector<Integer>();
> list.add(1);
> list.add(2);
>
> then I want to change the list to int[], but only list.toArray(new
> Integer[0]) works which gets an Integer[].
An Array [] is an object, essentially it works like any other object, if it
wraps primitives you have to get them out to use them. To go from
List<Integer> to int[] you must iterate explicitly.
You might also ask yourself why do I have List<Integer> and do I really want
int[]?
--
Mike W
VisionSet - 08 May 2006 14:17 GMT
> int can be auto-boxed into Integer in Tiger, but I want to convert
> int[] to Integer[]. How to do that? Thanks.
int[] myInts; // and assigned as req'd
Integer[] myIntegers = new Integer[myInts.length];
int i = 0;
for(int val : myInts) myIntegers[i++] = val;
--
Mike W
James McGill - 08 May 2006 16:42 GMT
> int can be auto-boxed into Integer in Tiger, but I want to convert
> int[] to Integer[]. How to do that? Thanks.
There's no magic that will do this. Whether there should be, is a
matter for debate. If you are doing this so often that it's a problem,
write a utility function. If it's a performance issue, rethink your
design so that you don't have to do this expensive conversion.
Roedy Green - 08 May 2006 19:08 GMT
>int can be auto-boxed into Integer in Tiger, but I want to convert
>int[] to Integer[]. How to do that? Thanks.
Allocate a new array and convert each element in a loop. see
http://mindprod.com/applets/conversion.html
The other way is much more drastic. Use a pure OO language where
there is no logical distinction between int and Integer and the
compiler does it as an optimisation.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.