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

2019-10-19 19:17发布

问题:

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.



标签: java class stack