Friday 22 June 2012

Convert Object array to Primitive array vice versa

Problem: Convert Object array to Primitive array vice versa.
Solution: ArrayUtils in apache commons lang can be used to do the same.



 
package com.lac;

import org.apache.commons.lang.ArrayUtils;

public class Conversion {
 public static void main(String[] args) {
  int i[] = new int[] { 1, 2, 3, 4 };
  
  // Convert Primitive int array to Object Integer Array
  Integer obj[] = ArrayUtils.toObject(i);

  // Convert Object array to Primitive array
  i = ArrayUtils.toPrimitive(obj);

 }
}