Convert intArray (int[]) to integerArray (Integer[]) and vice versa with and without Commons Library in java


In this tutorial we will learn how to Convert intArray (int[]) to integerArray (Integer[]) and Convert integerArray (Integer[]) to intArray (int[]) with and without using Commons Library in java with program and examples.

Contents of page >
1. Convert intArray (int[]) to integerArray (Integer[]) in java
  • Program/Example 1.1 to Convert intArray (int[]) to integerArray (Integer[]) in java.
  • Program/Example 1.2 to Convert intArray (int[]) to integerArray (Integer[]) Using Commons Library in java.



2. Convert integerArray (Integer[]) to intArray (int[]) in java
  • Program/Example 2.1 to Convert integerArray (Integer[]) to intArray (int[]) in java.
  • Program/Example 2.2 to Convert integerArray (Integer[]) to intArray (int[]) Using Commons Library in java.


1. Convert intArray (int[]) to integerArray (Integer[]) in java
Program/Example 1.1 to Convert intArray (int[]) to integerArray (Integer[]) in java.

public class intArrayToIntegerArrayConversionExample {
   public static void main(String[] args) {
          /**
          * Convert intArray (int[]) to integerArray (Integer[])
          */
          int intArray[] = { 3, 4, 7 };
          Integer integerArray[] = new Integer[intArray.length];
          for (int i = 0; i < intArray.length; i++) {
                 //Autoboxing will be performed, int will automatically converted to Integer
                 integerArray[i] = intArray[i];
          }
          System.out.println("Display integerArray (Integer[])  formed from intArray (int[])");
          for (Integer integer : integerArray) {
                 System.out.print(integer+" ");
          }
   }
}
/*output
Display integerArray (Integer[])  formed from intArray (int[])
3 4 7
*/



Program/Example 1.2 to Convert intArray (int[]) to integerArray (Integer[]) Using Commons Library in java.

import org.apache.commons.lang3.ArrayUtils;
/**
* Convert intArray (int[]) to integerArray (Integer[]) Using Commons Library
*/
public class intArrayToIntegerArrayConversionUsingCommonsLibraryExample {
   public static void main(String[] args) {
          int intArray[] = { 3, 4, 7 };
         
          //convert intArray (int[]) to integerArray (Integer[])
          //using ArrayUtils.toObject(intArray) method of Commons Library
          Integer integerArray[] = ArrayUtils.toObject(intArray);
         
          System.out.println("Display integerArray (Integer[])  formed "
                       + "from intArray (int[]) using "
                       + "ArrayUtils.toObject(intArray) "
                       + "method of Commons Library");
          for (Integer integer : integerArray) {
                 System.out.print(integer+" ");
          }
   }
}
/*output
Display integerArray (Integer[])  formed from intArray (int[]) using ArrayUtils.toObject(intArray) method of Commons Library
3 4 7
*/



2. Convert integerArray (Integer[]) to intArray (int[]) in java >

Program/Example 2.1 to Convert integerArray (Integer[]) to intArray (int[]) in java.

public class IntegerArrayTointArrayConversionExample {
   public static void main(String[] args) {
          /**
          * Convert integerArray (Integer[]) to intArray (int[])
          */
          Integer[] integerArray = { 3, 4, 7 };
          int[] intArray = new int[integerArray.length];
          for (int i = 0; i < integerArray.length; i++) {
                 //Unboxing will be performed, Integer will be automatically converted to int
                 intArray[i] = integerArray[i];
          }
          System.out.println("Display intArray (int[]) formed from integerArray (Integer[]) ");
          for (int i : intArray) {
                 System.out.print(i+" ");
          }
   }
}
/*output
Display intArray (int[]) formed from integerArray (Integer[])
3 4 7
*/

Program/Example 2.2 to Convert integerArray (Integer[]) to intArray (int[]) Using Commons Library in java.

import org.apache.commons.lang3.ArrayUtils;
/**
* Convert integerArray (Integer[]) to intArray (int[]) Using Commons Library
*/
public class IntegerArrayTointArrayConversionUsingCommonsLibraryExample {
   public static void main(String[] args) {
          Integer[] integerArray = { 3, 4, 7 };
         
          //Convert integerArray (Integer[]) to intArray (int[]) Using
      //ArrayUtils.toPrimitive(integerArray) Commons Library
          int[] intArray = ArrayUtils.toPrimitive(integerArray);
          System.out.println("Display intArray (int[]) formed from integerArray (Integer[]) using "
                       + "ArrayUtils.toPrimitive(integerArray) "
                       + "method of Commons Library ");
          for (int i : intArray) {
                 System.out.print(i+" ");
          }
   }
}
/*output
Display intArray (int[]) formed from integerArray (Integer[]) using ArrayUtils.toPrimitive(integerArray) method of Commons Library
3 4 7
*/



Summary -
So in this tutorial we learned how to Convert intArray (int[]) to integerArray (Integer[]) and Convert integerArray (Integer[]) to intArray (int[]) with and without using Commons Library in java with program and examples.




Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

How to convert String to float and float to string in java




String to double and vice versa >

How to convert String to double and double to string in java

How to convert double to String in java



String to short and vice versa >

How to convert String to short and short to string in java


How to convert String to char AND String to char array in java


eEdit
Must read for you :