Declaring a LinkedList in Java

前端 未结 11 2000
北荒
北荒 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条回答
  •  别那么骄傲
    2021-02-20 14:32

    Isn't LinkedList ob = new LinkedList() 100% correct?

    Well I'd suggest using the generic form, but sure - if you want to use functionality which is specific to LinkedList, you need to declare the variable accordingly.

    You might want to check whether the Deque or Queue interfaces have what you want though. If they do, use those in-keeping with the idea of describing what you need rather than what implementation you'll use.

提交回复
热议问题