How to get the Array Class for a given Class in Java?

前端 未结 5 868
时光取名叫无心
时光取名叫无心 2020-11-28 15:42

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this:<

5条回答
  •  旧巷少年郎
    2020-11-28 16:11

    You can get it using the class name. Just make sure you get the class using it's ClassLoader

        Class klass = yourClass;
        boolean init = wantToDoStaticOperations;
        Class.forName("[L" + klass.getName() + ";", init, klass.getClassLoader());
    

提交回复
热议问题