Convert int[] to Integer[] and vice versa in java

Here is a simple example to convert primitive int array to Integer array and vice versa

Using regular for-loop

<br />
package com.programtalk.learn.java.beginner;<br />
//author programtalk.com<br />
public class PrimitiveObjectArrayConversion {</p>
<p>	public static void main(String[] args) {</p>
<p>		System.out.println(&quot;######### Convert Primitive int[] to Integer[] ##########&quot;);<br />
		int[] prmitiveArray = new int[] { 10, 11, 12 };<br />
		Integer[] objectArray = convert2ObjectArray(prmitiveArray);</p>
<p>		System.out.println(&quot;Print converted Array&quot;);<br />
		for (Integer eachInteger : objectArray) {<br />
			System.out.println(eachInteger.getClass() +&quot; :&quot; +eachInteger);<br />
		}</p>
<p>		System.out.println(&quot;\n######### Convert Object Integer[] to Primitive int[]  ##########&quot;);<br />
		System.out.println(&quot;Print converted Array&quot;);</p>
<p>		Integer[] obj2 = new Integer[] { 20, 21, 22 };<br />
		int[] newObj2 = convert2PrimitiveArray(obj2);</p>
<p>		for (int eachPrimitiveInt : newObj2) {<br />
			System.out.println(&quot;primitive int : &quot; + eachPrimitiveInt);<br />
		}</p>
<p>	}</p>
<p>	public static Integer[] convert2ObjectArray(int[] intArray) {</p>
<p>		Integer[] result = new Integer[intArray.length];<br />
		for (int i = 0; i &lt; intArray.length; i++) {<br />
			result[i] = Integer.valueOf(intArray[i]);<br />
		}<br />
		return result;</p>
<p>	}</p>
<p>	public static int[] convert2PrimitiveArray(Integer[] integerArray) {</p>
<p>		int[] result = new int[integerArray.length];<br />
		for (int i = 0; i &lt; integerArray.length; i++) {<br />
			result[i] = integerArray[i].intValue();<br />
		}<br />
		return result;<br />
	}</p>
<p>}<br />

And the output is :

<br />
######### Convert Primitive int[] to Integer[] ##########<br />
Print converted Array<br />
class java.lang.Integer :10<br />
class java.lang.Integer :11<br />
class java.lang.Integer :12</p>
<p>######### Convert Object Integer[] to Primitive int[]  ##########<br />
Print converted Array<br />
primitive int : 20<br />
primitive int : 21<br />
primitive int : 22<br />

Java 8

Using Stream APIs of Java 8.

<br />
package com.programtalk.learn.java.beginner;</p>
<p>import java.util.Arrays;<br />
//author programtalk.com<br />
public class PrimitiveObjectArrayConversionJava8 {</p>
<p>	public static void main(String[] args) {</p>
<p>		System.out.println(&quot;######### Convert Primitive int[] to Integer[] ##########&quot;);<br />
		int[] prmitiveArray = new int[] { 10, 11, 12 };<br />
		Integer[] objectArray = convert2ObjectArray(prmitiveArray);</p>
<p>		System.out.println(&quot;Print converted Array&quot;);<br />
		for (Integer eachInteger : objectArray) {<br />
			System.out.println(eachInteger.getClass() +&quot; :&quot; +eachInteger);<br />
		}</p>
<p>		System.out.println(&quot;\n######### Convert Object Integer[] to Primitive int[]  ##########&quot;);<br />
		System.out.println(&quot;Print converted Array&quot;);</p>
<p>		Integer[] obj2 = new Integer[] { 20, 21, 22 };<br />
		int[] newObj2 = convert2PrimitiveArray(obj2);</p>
<p>		for (int eachPrimitiveInt : newObj2) {<br />
			System.out.println(&quot;primitive int : &quot; + eachPrimitiveInt);<br />
		}</p>
<p>	}</p>
<p>	public static Integer[] convert2ObjectArray(int[] intArray) {<br />
		return Arrays.stream(intArray).boxed().toArray( Integer[]::new );</p>
<p>	}</p>
<p>	public static int[] convert2PrimitiveArray(Integer[] integerArray) {<br />
		return Arrays.stream(integerArray).mapToInt(Integer::intValue).toArray();<br />
	}</p>
<p>}<br />

Commons Arrayutils

If you are not using java 8 and still want to have a oneliner. Use ArrayUtils from commons-lang3.

</p>
<p>package com.programtalk.learn.java.beginner;</p>
<p>import org.apache.commons.lang3.ArrayUtils;<br />
//author programtalk.com<br />
public class PrimitiveObjectArrayConversionArrayUtils {</p>
<p>	public static void main(String[] args) {</p>
<p>		System.out.println(&quot;######### Convert Primitive int[] to Integer[] ##########&quot;);<br />
		int[] prmitiveArray = new int[] { 10, 11, 12 };<br />
		Integer[] objectArray = convert2ObjectArray(prmitiveArray);</p>
<p>		System.out.println(&quot;Print converted Array&quot;);<br />
		for (Integer eachInteger : objectArray) {<br />
			System.out.println(eachInteger.getClass() +&quot; :&quot; +eachInteger);<br />
		}</p>
<p>		System.out.println(&quot;\n######### Convert Object Integer[] to Primitive int[]  ##########&quot;);<br />
		System.out.println(&quot;Print converted Array&quot;);</p>
<p>		Integer[] obj2 = new Integer[] { 20, 21, 22 };<br />
		int[] newObj2 = convert2PrimitiveArray(obj2);</p>
<p>		for (int eachPrimitiveInt : newObj2) {<br />
			System.out.println(&quot;primitive int : &quot; + eachPrimitiveInt);<br />
		}</p>
<p>	}</p>
<p>	public static Integer[] convert2ObjectArray(int[] intArray) {<br />
		return ArrayUtils.toObject(intArray);</p>
<p>	}</p>
<p>	public static int[] convert2PrimitiveArray(Integer[] integerArray) {<br />
		return ArrayUtils.toPrimitive(integerArray);<br />
	}</p>
<p>}</p>
<p>

References

Like this post? Don’t forget to share it!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.