Possible Duplicate:
Java: Instanceof and Genericsdlaczego instanceof nie działa z Generic?
Próbuję napisać funkcję, która rzuca ogólną listę do listy określonego typu. Znajdź poniższy kod
public <T>List<T> castCollection(List srcList, Class<T> clas){
List<T> list =new ArrayList<T>();
for (Object obj : srcList) {
if(obj instanceof T){
...
}
}
return list;
}
Ale obj instanceof T
pokazując błąd kompilacji -
Cannot perform instanceof check against type parameter T. Use instead its erasure Object >instead since further generic type information will be erased at runtime.
żadnych wyjaśnień lub sposób, aby uzyskać pożądany rezultat?
Z góry dziękuję. :)
.. ponieważ nie wyszukasz (-1); spróbuj '[java] ogólny instanceof' http://stackoverflow.com/questions/1570073/java-instanceof-and-generics, http://stackoverflow.com/questions/13768049/use-instanceof-in-a-eneric- way, http://stackoverflow.com/questions/8741984/why-t-instanceof-t-is-not-allowed-where-t-is-a-type-parametr-and-t-is-a-varia? lq = 1, http://stackoverflow.com/questions/4397660/generics-and-instanceof-java?rq=1 (etc) –