java Stack.capacity() 和 Stack.size() 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42884434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:56:09  来源:igfitidea点击:

Difference between Stack.capacity() and Stack.size()

javastack

提问by bryancresswell

I am currently doing a check with the Stack<E>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<E>is full?

我目前正在检查Stack<E>班级是否已满。但是, List 没有isFull()实现,所以我要求检查是否capacity()size(). 根据文档,size()返回此向量中的组件数,容量返回向量的当前容量。如果我理解正确,它们是一样的吗?如果是这样,我该如何检查我的Stack<E>是否已满?

回答by developer

stack.size()- gives the current size i.e., total number of elements pushed to the stack

stack.size()- 给出当前大小,即压入堆栈的元素总数

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.

stack.capacity()- 给出当前容量,即数组大小,如 10 或 20 等......即,一旦您将 10 个元素推入堆栈,您的堆栈容量就会翻倍。

Internally Stackuses Vectorand Vectoris a dynamic growing array. Also, for a Stack, you can't manually set the capacityIncrementfactor, rather the stack itself manages internally, you can look here

内部Stack使用Vector并且Vector是一个动态增长的数组。此外,对于 a Stack,您不能手动设置capacityIncrement因子,而是堆栈本身在内部管理,您可以查看here

回答by Tom Taylor

The Stackdatastructure in Java represents a last-in-first out (LIFO) stack of objects. It extends class Vectorwith five operation such as

StackJava 中的数据结构表示对象的后进先出 (LIFO) 堆栈。它Vector通过五个操作扩展了类,例如

  1. push
  2. pop
  3. peek item at the top of the stack
  4. Check stack is empty and
  5. search for an item in the stack
  1. 流行音乐
  2. 查看堆栈顶部的项目
  3. 检查堆栈为空并且
  4. 在堆栈中搜索一个项目

when the Stack classs would be like as follows

当 Stack 类如下所示

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

Size- 堆栈当前包含的元素数

Capacity- 它能够容纳的元素数量

The Pushoperation is implemented as follows

Push操作是这样实现的

public E push(E item) {
    addElement(item);

    return item;
}

addElementmethod belongs to Vectorclass which helps to insert a new element into the Vector

addElement方法属于Vector有助于将新元素插入到Vector

public synchronized void addElement(E obj) {
    modCount++;
    ensureCapacityHelper(elementCount + 1);
    elementData[elementCount++] = obj;
}

ensureCapacityHelperallows to check whether the Vectorinside is capable of adding a new element or not. If it does not have enough space to hold the new element the Vectorgrows

ensureCapacityHelper允许检查Vector内部是否能够添加新元素。如果它没有足够的空间来容纳新元素的Vector增长

 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.copyOfis a native method would allocate a new memory space with newCapacityand copies the data from old memory location to new location.

Arrays.copyOf是一种本机方法,它将分配一个新的内存空间,newCapacity并将数据从旧内存位置复制到新位置。

回答by john16384

The sizeis the current number of elements in the stack.

size是在堆叠元件的当前数目。

The capacityis 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.

capacity是内部的细节,告诉你,将适合的最大项目Vector。然而,这并不重要,因为它会在达到容量时自动扩展。

回答by Max Kryvych

Stack extends Vector and you can check javadocfor this class.

Stack extends Vector,您可以查看此类的javadoc

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)

int size() -> 返回此向量中的分量数。(向量中的最大元素)

int capacity() -> 返回此向量的当前容量。(向量中的当前元素)

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.

java.util.Vector 中的 capacity() 和 size() 之间的区别在于 capacity() 返回它可以存储的元素数量,而 size() 给出方法调用时 Vector 中的元素数量。向量的容量通常在构造函数本身中声明。