How to correctly return a shared array to a common Java method?

I have below a generic method that returns a generic array:

public static <T> T[] genericMethod1(List<T> input) {
    T[] res = (T[]) new Object[input.size()];

    int i = 0;
    for (T t : input) {
        res[i] = t;
        i++;
    }
    return res;
}

public static <T> T genericMethod2(List<T> input) {
    return input.get(0);
}

But later, when I try to get an array of results with:

LinkedList<Integer> list = new LinkedList<Integer>();
list.addFirst(1);
list.addFirst(1);

Integer[] i = (Integer[]) genericMethod1(list);  // 1) Runtime error
Integer j = genericMethod2(list);        // 2) works

In case 1, a runtime error always occurs:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;

Can anyone explain why and how to correctly return a shared array? Thanks.

Below is my understanding, please correct me if I am wrong.

As Tim mentioned, type erasure occurred during compilation, so in a byte encoder every T object is simply an Object type, and meanwhile, the compiler will add the cast type from Object to T “correctly”.

Let's say that T is an integer where T is declared, this is Object. For where he referenced, he introduces the type (implicitly) in T.

, T [], Object [], , Object []. [].

+4
3

, , , . genericMethod() , :

public static Object[] genericMethod(List input) {
    Object[] res = new Object[input.size()];

    int i = 0;
    for (Object t : input) {
        res[i] = t;
        i++;
    }
    return res;
}

, Object. Object[] Integer[], . , , Array.newInstance(). :

public static <T> T[] genericMethod(Class<T> clazz, List<T> input) {
    @SuppressWarnings("unchecked")
    T[] res = (T[]) Array.newInstance(clazz, input.size());

    int i = 0;
    for (T t : input) {
        res[i] = t;
        i++;
    }
    return res;
}

:

LinkedList<Integer> list = new LinkedList<Integer>();    
Integer[] i = genericMethod(Integer.class, list);

Update:

, genericMethod2(), :

public static Object genericMethod2(List input) {
    return input.get(0);
}

, Object. :

Integer j = genericMethod2(list);

genericMethod2() Integer:

Integer j = (Integer)genericMethod2(list);

, Integer Object, , , , Integer. , .

+11

genericMethod , , . Object .

    List<Integer> input = new ArrayList<Integer>();
    input.add(1);
    Object[] output = genericMethod(input);
    for(Object obj : output){
        System.out.println("Value= "+ (Integer)obj);
    }

, .

, ARRAY GENERICS Java.

Update:

Java:

. ; . , , , . , . , , .

+2

, java.util.ArrayList.toArray(T[]). , , , .

:

    List<Integer> intList = new ArrayList<>();
    intList.add(Integer.valueOf(1));
    intList.add(Integer.valueOf(2));
    intList.add(Integer.valueOf(3));
    Integer[] array = intList.toArray(new Integer[] {});
    System.out.println(Arrays.toString(array));//Will Print [1, 2, 3]

ArrayList.toArray(T[]) . .

0

Source: https://habr.com/ru/post/1625443/


All Articles