Fermer2
squalylLe 23/09/2009 à 11:56
pourquoi je peux pas? (java 6)

import java.lang.reflect.Array;
public class Utils {
    public static <T> T[] concat(T[] a, T[] b) {
        T[] r=  (T[]) Array.newInstance(T.class, a.length+b.length);
        System.arraycopy(a, 0, r, 0, a.length);
        System.arraycopy(b, 0, r, a.length, b.length);
        return r;        
    }
}


ça me dit: cannot select from a type variable

apparemment faut faire un truc comme ça (source de la jvm):
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        T[] copy = ((Object)newType == (Object)Object[].class)
            ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }

mais je pige pas trop trifus

edit: ah, ok , il faut faire :

public static <T> T[] concat(T[] a, T[] b) {
        T[] r=  (T[]) Array.newInstance(a.getClass(), a.length+b.length);
        System.arraycopy(a, 0, r, 0, a.length);
        System.arraycopy(b, 0, r, a.length, b.length);
        return r;
    }



je le laisse pour google.

edit2 : ah ben merde ça marche pas avec les types natifs byte, int, etc triso