Collection.toArray() java.lang.ClassCastException
2022-09-02 11:58:05
import java.util.HashMap;
import java.util.Map;
public class Main
{
public static void main(String[] args)
{
Map<Integer,Class> map=new HashMap<Integer,Class>();
map.put(0,Main.class);
Class[] classes=(Class[])map.values().toArray();
for (Class c:classes)
System.out.println(c.getName());
}
}
我尝试在这一行中施法,但得到例外。Class[] classes=(Class[])map.values().toArray();
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Class;
at Main.main(Main.java:11)
问题是什么?