Main Tutorials

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

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
tal
1 year 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
9 years ago

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