java栈的底层实现_Java底层堆栈的实现

1)java底层的Stack是继承Vector的,也就是说是直接实现Collection的(满足后进先出)

class Stack extends Vector

2)向堆栈中压入数据

/**

* Pushes an item onto the top of this stack. This has exactly

* the same effect as:

*

 
  

* addElement(item)

*

* @param   item   the item to be pushed onto this stack.

* @return  the item argument.

* @see     java.util.Vector#addElement

*/

public E push(E item) {

addElement(item);

return item;

}

调用Vector的addElement方法

/**

* Adds the specified component to the end of this vector,

* increasing its size by one. The capacity of this vector is

* increased if its size becomes greater than its capacity.

*

*

This method is identical in functionality to the

* {@link #add(Object) add(E)}

* method (which is part of the {@link List} interface).

*

* @param   obj   the component to be added

*/

public synchronized void addElement(E obj) {

modCount++;

ensureCapacityHelper(elementCount + 1);

elementData[elementCount++] = obj;

}

最终的数据是存储在elementData这个Object数组中。

2)向堆栈中拿出数据

/**

* Removes the object at the top of this stack and returns that

* object as the value of this function.

*

* @return  The object at the top of this stack (the last item

*          of the Vector object).

* @throws  EmptyStackException  if this stack is empty.

*/

public synchronized E pop() {

E       obj;

int     len = size();

obj = peek();

removeElementAt(len - 1);

return obj;

}

调用Vector的removeElementAt方法。

/**

* Deletes the component at the specified index. Each component in

* this vector with an index greater or equal to the specified

* {@code index} is shifted downward to have an index one

* smaller than the value it had previously. The size of this vector

* is decreased by {@code 1}.

*

*

The index must be a value greater than or equal to {@code 0}

* and less than the current size of the vector.

*

*

This method is identical in functionality to the {@link #remove(int)}

* method (which is part of the {@link List} interface).  Note that the

* {@code remove} method returns the old value that was stored at the

* specified position.

*

* @param      index   the index of the object to remove

* @throws ArrayIndexOutOfBoundsException if the index is out of range

*         ({@code index < 0 || index >= size()})

*/

public synchronized void removeElementAt(int index) {

modCount++;

if (index >= elementCount) {

throw new ArrayIndexOutOfBoundsException(index + " >= " +

elementCount);

}

else if (index < 0) {

throw new ArrayIndexOutOfBoundsException(index);

}

int j = elementCount - index - 1;

if (j > 0) {

System.arraycopy(elementData, index + 1, elementData, index, j);

}

elementCount--;

elementData[elementCount] = null; /* to let gc do its work */

}

从removeElementAt的参数可以看出,首先出栈的是第len-1的元素。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值