Difference between Stack.capacity() and Stack.size()
来源:互联网 发布:魔术师约翰逊生涯数据 编辑:程序博客网 时间:2024/06/07 09:16
http://stackoverflow.com/questions/42884434/difference-between-stack-capacity-and-stack-size
I'm currently doing a check with the Stack class to see if it's full. However, List does not have a isFull() implementation, so I am asking to check if capacity() is the same as size(). According to the docs, size() returns the number of components in this vector, and capacity returns the current capacity of the vector. If I understand correctly, are they the same? And if so, how do I go about checking if my Stack is full?
4 Answers
The Stack
datastructure in Java represents a last-in-first out (LIFO) stack of objects. It extends class Vector
with five operation such as
- push
- pop
- peek item at the top of the stack
- Check stack is empty and
- search for an item in the stack
when the Stack classs would be like as follows
public class Stack extends Vector {}
When the stack is created it contains no items. Coming to stack capacity and size
Size
- Number of elements a stack contains at present
Capacity
- Number of elements it is capable of holding
The Push
operation is implemented as follows
public E push(E item) { addElement(item); return item;}
addElement
method belongs to Vector
class which helps to insert a new element into the Vector
public synchronized void addElement(E obj) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = obj;}
ensureCapacityHelper
allows to check whether the Vector
inside is capable of adding a new element or not. If it does not have enough space to hold the new element the Vector
grows
private void ensureCapacityHelper(int minCapacity) { // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity);} /** * The maximum size of array to allocate. * Some VMs reserve some header words in an array. * Attempts to allocate larger arrays may result in * OutOfMemoryError: Requested array size exceeds VM limit */private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity);}
Arrays.copyOf
is a native method would allocate a new memory space with newCapacity
and copies the data from old memory location to new location.
The size
is the current number of elements in the stack.
The capacity
is an internal detail that tells you the maximum items that would fit in the Vector
. However, this is not really relevant as it will expand automatically when the capacity is reached.
ArrayList
no longer exposes this information. – john16384Mar 19 at 8:46vector::shrink_to_fit
, was introduced to ease this. But it was not introduced until the 2011 standard! – J.H.Bonarius Mar 19 at 9:15 Stack extends Vector and you can check javadoc for this class.
int size() -> Returns the number of components in this vector.(max elements in vector)
int capacity() -> Returns the current capacity of this vector.(current elements in vector)
The difference between capacity() and size() in java.util.Vector is that capacity() returns how many elements it can store whereas the size() gives the no.of elements in the Vector at the time of method call. Capacity of a vector is usually declared within the constructor itself.
size
gives the amount of elements currently on the stack, whilecapacity
gives the current amount of reserved memory for the stack. If by adding elements the capacity is exceeded, the stack will be moved to a newly allocated large peice of memory. – J.H.Bonarius Mar 19 at 8:48stack.size()
- gives the current size i.e., total number of elements pushed to the stack
stack.capacity()
- gives the current capacity i.e., array size like 10 or 20 etc... i.e., as soon as you pushes 10 elements to the stack, your stack capacity gets doubled.
Internally Stack
uses Vector
and Vector
is a dynamic growing array. Also, for a Stack
, you can't manually set the capacityIncrement
factor, rather the stack itself manages internally, you can lookhere
Buddy memory allocation
en.wikipedia.org/wiki/Buddy_memory_allocation? – Rajasuba Subramanian Mar 19 at 8:53- Difference between Stack.capacity() and Stack.size()
- difference between stack and heap
- The difference between stack and heap
- Difference between Stack and Heap in Java
- the difference between kernel stack and user stack
- What's the difference between stack and heap?
- What's the difference between Heap and Stack
- Difference between Stack and Heap memory in Java
- What’s the difference between a stack and a heap?
- Difference between Stack and Heap memory in Java
- Java - Difference between Stack and Heap memory in Java
- CareerCup What is the difference between a computers heap and it's stack?
- The difference between stack and heap(堆和栈的区别)
- the difference of heap and stack
- Relation between the main stack and agent
- The difference of Heap and Stack(转贴)
- Comparison between Apache Axis2 and Apache cxf on stack flow
- The difference between (()) and ().
- 随手记录--线程
- Windows安装Pillow错误解决
- 漫谈C++:良好的编程习惯与编程要点
- 面向对象
- js时间戳、毫秒格式化
- Difference between Stack.capacity() and Stack.size()
- 数据结构—单链表
- 随手记录--xml in java
- BZOJ 1079 [SCOI2008] 着色方案
- 基于 Django1.10 文档的深入学习(28)—— Managing static files(e.g. images, JavaScript, CSS)
- 【C语言训练】委派任务
- Flask 使用消息闪烁(flash)报错
- 编写java程序151条建议读书笔记(6)
- js瀑布流
Stack
isn't bound to a predetermined size. – Elliott Frisch Mar 19 at 8:26push
operator that does this automatically. But then what: what so you want to do when the stack is full? Throw an exception? – J.H.Bonarius Mar 19 at 8:54