how to convert int[] to Integer[]

T

Thomas Hawtin

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
 
L

lionchao

int can be auto-boxed into Integer in Tiger, but I want to convert
int[] to Integer[]. How to do that? Thanks.
 
V

VisionSet

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;
 
L

lionchao

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[].
 
V

VisionSet

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

James McGill

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.
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top