Declaring a LinkedList in Java

前端 未结 11 2007
北荒
北荒 2021-02-20 14:26

I always learn when we declare a collection we should do, Interface ob = new Class(), if i want to use for example a LinkedList i\'ll do List ob = new LinkedL

11条回答
  •  -上瘾入骨i
    2021-02-20 14:55

    you can still have access to LinkedList methods by using List, all you have to do is to type cast for example

    ((LinkedList)ob).add()
    

    The point of using generic List and not LinkedList is because in case you simply change the type of lists you are using (let's say double linked list) your program will still work Generics are to simplify your code to be more portable and more "changeable"

提交回复
热议问题