In this Q&A, we'll go over how to box and unbox an array of primitives.
Integer primitive can be boxed as below:
int ip = 0;
Integer io = ip;
But the following code results in a compile error:
int[] ia = {1,2,3,4};
Integer[] IA2 = ia;
Integer primitive can be boxed as below:
int ip = 0;
Integer io = ip;
But the following code results in a compile error:
int[] ia = {1,2,3,4};
Integer[] IA2 = ia;
To achieve the above, Apache commons ArrayUtils provides methods to box and unbox an array. See code below:
int[] ia = {1,2,3,4};
Integer[] IA = ArrayUtils.toObject(ia);
int[] ia3 = ArrayUtils.toPrimitive(IA);
Better option - where possible - is to create an ArrayList. That's probably the reason, Java does not provide an easy way to box or unbox primitive arrays.
Comments
Post a Comment