How to increase the capacity of a stack? [closed]

不问归期 提交于 2019-12-13 11:22:49

问题


I'm using a Stack in Java. The problem is that I only can push 10 items into the stack, and I need to push 20 items.

How do I increment the capacity of the stack?


回答1:


Java's Stack class inherits from Vector and provides convenience methods to allow a Vector to behave like a stack. Since Vector grows naturally, there's no need to increase the capacity manually.

I'm guessing you're doing something else wrong. That, or I misunderstood your question. If you want a more accurate answer, please give more information, such as the code your using, what behavior are you expecting, what behavior are you getting, etc.




回答2:


The Java 7 Stack is not bound to any size. It is back by a Vector which says it is a "a growable array of objects".

You should be able to add as many object to the Stack as you like.




回答3:


Stack extends Vector which has a constructor that defines an initial capacity. There's also a method called ensureCapacity(int minCapacity) which could help you
but as the other posters said: you shouldn't have to do this manually. Maybe providing some code snippets could enlighten us all.



来源:https://stackoverflow.com/questions/8220016/how-to-increase-the-capacity-of-a-stack

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