在List,Set,Queue,Deque中转换数组int,boolean,double,long和String

时间:2015-09-01 11:36:23

标签: java object type-conversion

这是我的代码。我仅以示例实现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中基元类型之间进行转换的方法。我有四种方法,从原始转换为对象。如何在单一方法中实现?我需要从单一方法实现从原语转换为对象。感谢

2 个答案:

答案 0 :(得分:3)

你做不到。查看Arrays类 - 它有许多几乎相同的方法版本,例如copyOf()(其中一个链接:P)。这可能被视为Java中的一个设计缺陷,但它存在并且在可预见的未来并没有改变。另请查看this问题。作为旁注,请不要使用盒装类的构造函数 - 使用valueOf()方法,例如Double.valueOf()

答案 1 :(得分:-2)

使用以下方法从数组中获取所需的类型。 (将该数组的副本复制到所需类型)

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