Java – Convert int[] to Integer[] example

Examples show you how to convert between int[] and its’ wrapper class Integer[].

1. Convert int[] to Integer[]


	public static Integer[] toObject(int[] intArray) {

		Integer[] result = new Integer[intArray.length];
		for (int i = 0; i < intArray.length; i++) {
			result[i] = Integer.valueOf(intArray[i]);
		}
		return result;
	}

2. Convert Integer[] to int[]


	public static int[] toPrimitive(Integer[] IntegerArray) {

		int[] result = new int[IntegerArray.length];
		for (int i = 0; i < IntegerArray.length; i++) {
			result[i] = IntegerArray[i].intValue();
		}
		return result;
	}

The theory is simple, create a new array, and dump the old value inside.

1. Full Example

Review a full example to show you how it works.

ArrayConvertExample.java

package com.mkyong.test;

public class ArrayConvertExample {

	public static void main(String[] args) {

		int[] obj = new int[] { 1, 2, 3 };
		Integer[] newObj = toObject(obj);

		System.out.println("Test toObject() - int -> Integer");
		for (Integer temp : newObj) {
			System.out.println(temp);
		}

		Integer[] obj2 = new Integer[] { 4, 5, 6 };
		int[] newObj2 = toPrimitive(obj2);

		System.out.println("Test toPrimitive() - Integer -> int");
		for (int temp : newObj2) {
			System.out.println(temp);
		}

	}

	// Convert int[] to Integer[]
	public static Integer[] toObject(int[] intArray) {

		Integer[] result = new Integer[intArray.length];
		for (int i = 0; i < intArray.length; i++) {
			result[i] = Integer.valueOf(intArray[i]);
		}
		return result;

	}

	// Convert Integer[] to int[]
	public static int[] toPrimitive(Integer[] IntegerArray) {

		int[] result = new int[IntegerArray.length];
		for (int i = 0; i < IntegerArray.length; i++) {
			result[i] = IntegerArray[i].intValue();
		}
		return result;
	}

}

Output


Test toObject() - int -> Integer
1
2
3
Test toPrimitive() - Integer -> int
4
5
6

2. ArrayUtils Example

To save you time, uses the ArrayUtils class from the Apache common library, it does the same thing.

ArrayConvertExample.java

package com.mkyong.test;

import org.apache.commons.lang3.ArrayUtils;

public class ArrayConvertExample {

	public static void main(String[] args) {

		int[] obj = new int[] { 1, 2, 3 };
		Integer[] newObj = ArrayUtils.toObject(obj);

		System.out.println("Test toObject() - int -> Integer");
		for (Integer temp : newObj) {
			System.out.println(temp);
		}

		Integer[] obj2 = new Integer[] { 4, 5, 6 };
		int[] newObj2 =	ArrayUtils.toPrimitive(obj2);

		System.out.println("Test toPrimitive() - Integer -> int");
		for (int temp : newObj2) {
			System.out.println(temp);
		}

	}

Test toObject() - int -> Integer
1
2
3
Test toPrimitive() - Integer -> int
4
5
6

References

  1. Apache ArrayUtils JavaDoc
  2. Primitive wrapper class

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
tal
4 years ago

Hi! thank you so much mkyong, you helped me a lot through hard times and you are the only reason my parents still love me. keep up the good work!

shafi
12 years ago

how to write xml parse in java and display in html….. plz reply