Is there a clean way to assign the Class of a generic type to a variable?

后端 未结 2 757
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-19 17:44

Given this code:

List ints = new ArrayList();

// Type mismatch: 
// cannot convert from Class t         


        
2条回答
  •  长情又很酷
    2021-02-19 18:21

    One way to do this would be to create your own class. I am not sure if this would meet your needs.

    I am not really sure what you are trying to achieve. So perhaps wildcards is the answer.

    import java.util.*;
    
    class Main
    {
          public static void main(String[] args)
          {
                List list = new ArrayList();
                Class clazz = list.getClass();
                System.out.println(clazz) ;
          }
    }
    

提交回复
热议问题