问题
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