Java中int[]与Integer[]相互转化的方法

雨点打透心脏的1/2处 2023-01-13 04:29 228阅读 0赞

传统方法

  1. //Convert int[] to Integer[]
  2. public static Integer[] toObject(int[] intArray) {
  3. Integer[] result = new Integer[intArray.length];
  4. for (int i = 0; i < intArray.length; i++) {
  5. result[i] = Integer.valueOf(intArray[i]);
  6. }
  7. return result;
  8. }
  9. //Convert Integer[] to int[]
  10. public static int[] toPrimitive(Integer[] IntegerArray) {
  11. int[] result = new int[IntegerArray.length];
  12. for (int i = 0; i < IntegerArray.length; i++) {
  13. result[i] = IntegerArray[i].intValue();
  14. }
  15. return result;
  16. }

Apache Commons Lang 工具类 ArrayUtils

添加依赖

  1. <dependency>
  2. <groupId>org.apache.commons</groupId>
  3. <artifactId>commons-lang3</artifactId>
  4. <version>3.12.0</version>
  5. </dependency>
  6. import org.apache.commons.lang3.ArrayUtils;
  7. public class ArrayConvertExample {
  8. public static void main(String[] args) {
  9. int[] obj = new int[] { 1, 2, 3 };
  10. Integer[] newObj = ArrayUtils.toObject(obj);
  11. Integer[] obj2 = new Integer[] { 4, 5, 6 };
  12. int[] newObj2 = ArrayUtils.toPrimitive(obj2);
  13. Integer[] obj3 = new Integer[] { 7, 8, null };
  14. int[] newObj3 = ArrayUtils.toPrimitive(obj2, 0);
  15. }
  16. }

ArrayUtils.toObject()与ArrayUtils.toPrimitive()源码如下:

  1. package org.apache.commons.lang3;
  2. public class ArrayUtils {
  3. ...
  4. public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0];
  5. //Convert int[] to Integer[]
  6. public static Integer[] toObject(final int[] array) {
  7. if (array == null) {
  8. return null;
  9. } else if (array.length == 0) {
  10. return EMPTY_INTEGER_OBJECT_ARRAY;
  11. }
  12. final Integer[] result = new Integer[array.length];
  13. for (int i = 0; i < array.length; i++) {
  14. result[i] = Integer.valueOf(array[i]);
  15. }
  16. return result;
  17. }
  18. public static final int[] EMPTY_INT_ARRAY = new int[0];
  19. //Convert Integer[] to int[]
  20. public static int[] toPrimitive(final Integer[] array) {
  21. if (array == null) {
  22. return null;
  23. } else if (array.length == 0) {
  24. return EMPTY_INT_ARRAY;
  25. }
  26. final int[] result = new int[array.length];
  27. for (int i = 0; i < array.length; i++) {
  28. result[i] = array[i].intValue();
  29. }
  30. return result;
  31. }
  32. public static int[] toPrimitive(final Integer[] array, final int valueForNull) {
  33. if (array == null) {
  34. return null;
  35. } else if (array.length == 0) {
  36. return EMPTY_INT_ARRAY;
  37. }
  38. final int[] result = new int[array.length];
  39. for (int i = 0; i < array.length; i++) {
  40. final Integer b = array[i];
  41. result[i] = (b == null ? valueForNull : b.intValue());
  42. }
  43. return result;
  44. }
  45. ...
  46. }

Java 8 方法

  1. import java.util.Arrays;
  2. import java.util.stream.IntStream;
  3. public class ArrayConvertExample {
  4. public static void main(String[] args) {
  5. Integer[] obj = new Integer[] { null, 1, 2, 3};
  6. int[] newObj = Arrays.stream(obj).mapToInt(i -> i == null ? 0 : i.intValue()).toArray();
  7. //Integer[] obj = new Integer[] {1, 2, 3};
  8. //int[] newObj = Arrays.stream(obj).mapToInt(Integer::valueOf).toArray();
  9. int[] obj2 = new int[] { 4, 5, 6};
  10. Integer[] newObj2 = Arrays.stream(obj2).boxed().toArray(Integer[]::new);
  11. Integer[] newObj3 = IntStream.of(obj2).boxed().toArray(Integer[]::new);
  12. }
  13. }

小结

个人认为Java 8方法最佳,即不用编写过多代码,也不用添加新依赖。

其他基本类型数组与其包装类型数组之间转换都用差不多的方法,大家可以触类旁通,举一反三。

参考资料

  1. Java – Convert int[] to Integer[] example
  2. ArrayUtils (Apache Commons Lang 3.11 API)
  3. How to convert Integer[] to int[] array in Java?
  4. How to convert int[] to Integer[] in Java?

发表评论

表情:
评论列表 (有 0 条评论,228人围观)

还没有评论,来说两句吧...

相关阅读