Java中int[]与Integer[]相互转化的方法
生活随笔
收集整理的這篇文章主要介紹了
Java中int[]与Integer[]相互转化的方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
傳統方法
//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; }Apache Commons Lang 工具類 ArrayUtils
添加依賴
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.12.0</version> </dependency> 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);Integer[] obj2 = new Integer[] { 4, 5, 6 };int[] newObj2 = ArrayUtils.toPrimitive(obj2);Integer[] obj3 = new Integer[] { 7, 8, null };int[] newObj3 = ArrayUtils.toPrimitive(obj2, 0);} }ArrayUtils.toObject()與ArrayUtils.toPrimitive()源碼如下:
package org.apache.commons.lang3;public class ArrayUtils {...public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0];//Convert int[] to Integer[]public static Integer[] toObject(final int[] array) {if (array == null) {return null;} else if (array.length == 0) {return EMPTY_INTEGER_OBJECT_ARRAY;}final Integer[] result = new Integer[array.length];for (int i = 0; i < array.length; i++) {result[i] = Integer.valueOf(array[i]);}return result;}public static final int[] EMPTY_INT_ARRAY = new int[0];//Convert Integer[] to int[]public static int[] toPrimitive(final Integer[] array) {if (array == null) {return null;} else if (array.length == 0) {return EMPTY_INT_ARRAY;}final int[] result = new int[array.length];for (int i = 0; i < array.length; i++) {result[i] = array[i].intValue();}return result;}public static int[] toPrimitive(final Integer[] array, final int valueForNull) {if (array == null) {return null;} else if (array.length == 0) {return EMPTY_INT_ARRAY;}final int[] result = new int[array.length];for (int i = 0; i < array.length; i++) {final Integer b = array[i];result[i] = (b == null ? valueForNull : b.intValue());}return result;}...}Java 8 方法
import java.util.Arrays; import java.util.stream.IntStream;public class ArrayConvertExample {public static void main(String[] args) {Integer[] obj = new Integer[] {null, 1, 2, 3};int[] newObj = Arrays.stream(obj).mapToInt(i -> i == null ? 0 : i.intValue()).toArray();//Integer[] obj = new Integer[] {1, 2, 3};//int[] newObj = Arrays.stream(obj).mapToInt(Integer::valueOf).toArray();int[] obj2 = new int[] {4, 5, 6};Integer[] newObj2 = Arrays.stream(obj2).boxed().toArray(Integer[]::new);Integer[] newObj3 = IntStream.of(obj2).boxed().toArray(Integer[]::new);}}小結
個人認為Java 8方法最佳,即不用編寫過多代碼,也不用添加新依賴。
其他基本類型數組與其包裝類型數組之間轉換都用差不多的方法,大家可以觸類旁通,舉一反三。
參考資料
總結
以上是生活随笔為你收集整理的Java中int[]与Integer[]相互转化的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《剑指Offer》38:字符串的排列
- 下一篇: 《UNIX环境高级编程 3rd》笔记(1