簡體   English   中英

在List,Set,Queue,Deque中轉換數組int,boolean,double,long和String

[英]Convert array int, boolean, double, long and String in List, Set, Queue, Deque

這是我的代碼。 我僅以實例List為例。

public class Main {

    public static Integer[] toObject(int[] array) {

        Integer[] result = new Integer[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = new Integer(array[i]);
        }
        return result;
        }

    public static Double[] toObject(double[] array) {

        Double[] result = new Double[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = new Double(array[i]);
        }
        return result;
        }

    public static Long[] toObject(long[] array) {

        Long[] result = new Long[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = new Long(array[i]);
        }
        return result;
        }

    public static Boolean[] toObject(boolean[] array) {

        Boolean[] result = new Boolean[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = new Boolean(array[i]);
        }
        return result;
        }

    public static <T> void fromArrayToCollection(T[] array, Collection<T> c) {
        for (T o : array) {
            c.add(o);
        }
    }

    public static void main(String[] args) {

        int [] i = new int[2];
        i[0] = 1;
        i[1] = 1;
        Integer [] ii = toObject(i);
        List<Integer> ic = new ArrayList<Integer>();
        fromArrayToCollection(ii, ic);
        ic.add(3);
        ic.add(4);
        System.out.println(ic);

        long [] l = new long[2];
        l[0] = 1L;
        l[1] = 2L;
        Long [] ll = toObject(l);
        List<Long> lc = new ArrayList<Long>();
        fromArrayToCollection(ll, lc);
        lc.add(3L);
        System.out.println(lc);

        double [] d = new double[2];
        d[0] = 1.0;
        d[1] = 2.0;
        Double [] dd = toObject(d);
        List<Double> dc = new ArrayList<Double>();
        fromArrayToCollection(dd, dc);
        dc.add(3.0);
        System.out.println(dc);

        boolean [] b = new boolean[2];
        b[0] = true;
        b[1] = false;
        Boolean [] bb = toObject(b);
        List<Boolean> bc = new ArrayList<Boolean>();
        fromArrayToCollection(bb, bc);
        bc.add(true);
        System.out.println(bc);

        String [] s = new String[2];
        s[0] = "One";
        s[1] = "Two";
        List<String> sc = new ArrayList<String>();
        fromArrayToCollection(s, sc);
        sc.add("Three");
        System.out.println(sc);

    }
}

Java不能具有原始數據類型的泛型。 為此,我編寫了在Object中基元類型之間進行轉換的方法。 我有四種方法,從原始轉換為對象。 如何在單一方法中實現? 我需要從單一方法實現從原語轉換為對象。 謝謝

你不能。 查看Arrays類 - 它有許多幾乎相同的方法版本,例如copyOf() (其中一個鏈接:P)。 這可能被視為Java中的一個設計缺陷,但它存在並且在可預見的未來並沒有改變。 還看看這個問題。 作為旁注,不要使用盒裝類的構造函數 - 使用valueOf()方法,例如Double.valueOf()

使用以下方法從數組中獲取所需的類型。 (將該數組的副本復制到所需類型)

 copyOf(U[] original, int newLength, Class<? extends T[]> newType)::java.util.Arrays

暫無
暫無

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

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