3

This is my Code. I implement List only for example.

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 can't have generics for primitive data types. For that i write methods that convert between primitive types in Object. I have four method that converted from primitive to object. How it could be implemented in a single method? I need that convert to object from primitive was implemented in single method. Thanks

  • 1
    You should not use `new Integer`, `new Boolean` and so on. Often it's just waste of memory. Use `Integer.valueOf`, `Boolean.valueOf` or implicit autoboxing (just `result[i] = array[i]`). – Tagir Valeev Sep 01 '15 at 14:09
  • By the way, `valueOf()` is generally better because rather than creating a new object every time, it special cases some values. – bcsb1001 Sep 01 '15 at 14:40

2 Answers2

3

You can't. Look at the Arrays class - it has many almost identical versions of its methods such as copyOf() (which one to link :P). This could be regarded as a design flaw in Java, but it's there and it's not changing in the forseeable future. Also look at this question. As a side note, don't use the constructors of boxed classes - use the valueOf() methods e.g. Double.valueOf().

Community
  • 1
  • 1
bcsb1001
  • 2,834
  • 3
  • 24
  • 35
-2

Use following method to get the desired type from an array. (making copy of that array to desired type)

 copyOf(U[] original, int newLength, Class<? extends T[]> newType)::java.util.Arrays
Shubham Chaurasia
  • 2,472
  • 2
  • 15
  • 22
  • This is _not_ an answer. What do you mean by that method? Use words and explain the solution to the OP's question - this isn't it. – bcsb1001 Sep 01 '15 at 14:29