Java generics: List = new LinkedList>() is prohibited?

前端 未结 3 1617
迷失自我
迷失自我 2021-01-04 12:44

How come, in Java, I can write

List list = new LinkedList();

but not

List>          


        
3条回答
  •  难免孤独
    2021-01-04 13:06

    Each parameterization must be wildcarded:

    List> list = new LinkedList>();
    

    Edit: I was searching through Angelika Langer's FAQ for an explanation, but didn't find one (it's probably there, but hiding somewhere in the 500 pages).

    The way to think about this is that each parameterization is independent, and the compiler will not infer information about the parameterization unless you explicitly say to do so.

提交回复
热议问题