Should stacks be made from arrays or linked list java?

*爱你&永不变心* 提交于 2020-02-25 05:23:45

问题


I have to write a stack for class and while I understand the concept of how a stack works, I wasn't told if they are made using an array or a linked list or something else? How are most stacks created?


回答1:


ArrayDeque is a solid class implementation of the stack concept. This class has implemented stack in the most efficient way. Please look at the class implementation for the details of various methods.

http://www.docjar.com/html/api/java/util/ArrayDeque.java.html

More specifically, look at public E pollFirst(){...} and public void addFirst(E e)




回答2:


java.util.Stack is a subclass of java.util.Vector which was a Thread-safe precursor to ArrayList. Hope that helps.




回答3:


Both options, array and linked list are appropiate.

A linked list may be simpler because you needn't worry about the array size. An array based implementation on the other hand may have better runtime behavior and can be easier to debug (because its easier to view the array than a linked list in the debugger).

Chose whatever you're comfortable with.



来源:https://stackoverflow.com/questions/26263376/should-stacks-be-made-from-arrays-or-linked-list-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!