簡體   English   中英

將對象數組轉換為其原始類型數組

[英]Converting an array of objects to an array of their primitive types

如果您有一組具有原始類型(例如 Byte、Integer、Char 等)的 Java 對象。 有沒有一種巧妙的方法可以將其轉換為原始類型的數組? 特別是可以在不必創建新數組並循環遍歷內容的情況下完成此操作。

例如,給定

Integer[] array

將其轉換為最簡潔的方法是什么

int[] intArray

不幸的是,當在 Hibernate 和一些我們無法控制的第三方庫之間進行交互時,這是我們必須經常做的事情。 看起來這將是一個很常見的操作,所以如果沒有捷徑,我會感到驚訝。

謝謝你的幫助!

Apache Commons Lang再次成為您的朋友。 他們提供了ArrayUtils.toPrimitive()來滿足你的需求。 您可以指定處理空值的方式。

使用 Java 8 中引入的可以做到這一點:

int[] intArray = Arrays.stream(array).mapToInt(Integer::intValue).toArray();

但是,目前只有intlongdouble原始流。 如果您需要轉換為另一種基本類型,例如byte則無需外部庫的最短方法是:

byte[] byteArray = new byte[array.length];
for(int i = 0; i < array.length; i++) byteArray[i] = array[i];

或者,如果需要,可以將 for 循環替換為流:

IntStream.range(0, array.length).forEach(i -> byteArray[i] = array[i]);

如果您的任何元素為null所有這些都將拋出NullPointerException

不幸的是,Java 平台中沒有任何東西可以做到這一點。 順便說一句,您還需要顯式處理Integer[]數組中的null元素(您將使用什么int ?)。

使用番石榴

int[] intArray = Ints.toArray(Arrays.asList(array));

文檔:

特別是可以在不必創建新數組並循環遍歷內容的情況下完成此操作。

在 Java 中不能將 Integer 數組轉換為 int(即不能更改數組元素的類型)。 因此,您必須創建一個新的 int[] 數組並將 Integer 對象的值復制到其中,或者您可以使用適配器:

class IntAdapter {
    private Integer[] array;
    public IntAdapter (Integer[] array) { this.array = array; }
    public int get (int index) { return array[index].intValue(); }
}

這可以使您的代碼更具可讀性,並且 IntAdapter 對象將只消耗幾個字節的內存。 適配器的一大優勢是您可以在此處處理特殊情況:

class IntAdapter {
    private Integer[] array;
    public int nullValue = 0;
    public IntAdapter (Integer[] array) { this.array = array; }
    public int get (int index) { 
        return array[index] == null ? nullValue : array[index].intValue();
    }
}

另一種解決方案是使用包含許多預定義適配器的Commons Primitives 在您的情況下,請查看ListIntList

或者如果你只做一次,就用簡單的方法來做。 但是你還沒有談到 Integer!=null 的情況。

    //array is the Integer array
    int[] array2 = new int[array.length];
    int i=0;
    for (Integer integer : array) {
        array2[i] = integer.intValue();
        i++;
    }

使用Dollar很簡單:

Integer[] array = ...;
int[] primitiveArray = $(array).toIntArray();

這是所有原始類型的通用解決方案

/**
 * Convert Collection to equivalent array of primitive type
 * @param <S> [in] Object type of source collection
 * @param tcls [in] class of the primitive element
 * @param q [in] source collection
 * @return Equivalent Array of tcls-elements, requires cast to "tcls"[]
 */
public static <S> Object asPrimitiveArray(Class<?> tcls, Collection<S> q)
{
    int n = q.size();
    Object res = Array.newInstance(tcls, n);
    Iterator<S> i = q.iterator();
    int j = 0;
    while (i.hasNext())
    {
        Array.set(res, j++, i.next());
    }
    return res;
}

/**
 * Convert Object array to equivalent array of primitive type
 * @param <S> [in] Object type of source array
 * @param tcls [in] class of the primitive element
 * @param s [in] source array
 * @return Equivalent Array of tcls-elements, requires cast to "tcls"[]
 */
public static <S> Object asPrimitiveArray(Class<?> tcls, S[] s)
{
    return asPrimitiveArray(tcls, Arrays.asList(s));
} 

整數到整數的轉換

Integer[] a = ...
int[] t = (int[]) asPrimitiveArray(int.class, a);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM