Specifying a generic type in java from Class object

后端 未结 7 1502
野的像风
野的像风 2020-12-11 16:19

Why this is wrong:

    Class type = Integer.class;
    ArrayList = new ArrayList<>();

?

I

相关标签:
7条回答
  • 2020-12-11 16:35

    Taken literally, the other answers' suggestions of how to implement createAList are ignoring something important: due to type erasure, such a method is pointless.

    Given you want a List<? extends Number>, you can just write this:

    List<? extends Number> lst = new ArrayList<>();
    

    If you just wanted a List<?>, you could write:

    List<?> lst = new ArrayList<>();
    

    If you were in the scope of a type parameter T and wanted a List<T>, you could write:

    List<T> lst = new ArrayList<>();
    

    Notice that a Class object has nothing to do with these constructor calls, just like the methods in the other answers. That's because at runtime, the ArrayList instance doesn't know or care about whatever generic type its references had at runtime.

    0 讨论(0)
  • 2020-12-11 16:36

    An ArrayList<> has to have a specific type it holds . You can put objects of that type or any sub-type in it though.

    So use

    List<Number> = new ArrayList<Number>();
    

    and you can put Integers in it

    Notice how I used the interface on the left and the class on the right of the equal sign. That's a best practice sort of thing.

    If you want a list that will just hold Integer (as per your example) the answer by @irreputable is your best bet. This answer will hold Integer but not just Integer.

    0 讨论(0)
  • 2020-12-11 16:37

    That's not how you use generics. You don't use a Class object, you use the class name directly in your code.

    ArrayList<Integer> = new ArrayList<>();
    
    0 讨论(0)
  • 2020-12-11 16:37

    Feel the difference between java Class (which actually generic too) object and class name.

    You should use class name specifying generic type.

    ArrayList<Number> = new ArrayList<>();
    // ArrayList<Number.class> = new ArrayList<>(); <- WRONG
    

    UPD:

    Use this approach if you'll know type only in runtime:

    public <T extends Number> void createAList(Class<T> type) {
        ArrayList<T> toReturn = new ArrayList<>();
        return toReturn;
    }
    
    0 讨论(0)
  • 2020-12-11 16:49
    ArrayList<type> = new ArrayList<>();
    

    this line is wrong. First, you missed identifier (variable name) here; Second, you mixed the concepts of "type" and "class". You can delcare

    ArrayList<Integer> list = new ArrayList<>();
    

    But according to yours, type = Integer.class. Obviously, Integer is not equivalent to Integer.class. Similarly you can't have Integer.class i = 1; The former one is a "type", the second one is a "Class" object.

    You can create a generic method:

    public <T extends Number> List<T> createAList (Class<T> type) {
        return new ArrayList<T>();
    }
    
    0 讨论(0)
  • 2020-12-11 16:51
    <T extends Number> ArrayList<T> createAList(Class<T> type)  
    {
        ArrayList<T> toReturn = new ArrayList<>();
        return toReturn;
    }
    
    
    ArrayList<Integer> intList = createAList(Integer.class);
    
    0 讨论(0)
提交回复
热议问题