关于stack



  • 请问老师及助教 强化课视频资料里的stack题目在实现的时候都是用Deque stack = new LinkedList(); 为什么不直接用java里的stack呢?



  • 分享一下我是如何寻找答案的,我会google关键词:deque stack java

    https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=deque stack java

    第一个链接就在这里:

    http://stackoverflow.com/questions/12524826/why-should-i-use-deque-over-stack

    引用其中高分的答案:

    For one thing, it’s more sensible in terms of inheritance. The fact that Stack extends Vector is really strange, in my view. Early in Java, inheritance was overused IMO - Properties being another example.

    For me, the crucial word in the docs you quoted is consistent. Deque exposes a set of operations which is all about being able to fetch/add/remove items from the start or end of a collection, iterate etc - and that’s it. There’s deliberately no way to access an element by position, which Stack exposes because it’s a subclass of Vector.

    Oh, and also Stack has no interface, so if you know you need Stack operations you end up committing to a specific concrete class, which isn’t usually a good idea.


登录后回复
 

与 BitTiger Community 的连接断开,我们正在尝试重连,请耐心等待