Java基础之源码学习——Stack


Stack继承Vector,是一种“后进先出”(LIFO)的数据结构,只能在一端进行插入或者删除数据的操作。

除了父类Vector的方法和stack的构造方法(默认长度是Vector的默认长度外,stack还有以下5个方法:

  1. empty(): boolean
    Returns whether the stack is empty or not.
    返回改Stack是否为空:当stack中不包含任何项时返回true,否则返回false。

  2. peek(): E
    Returns the element at the top of the stack without removing it.
    查看并返回栈顶的项,并没有移除该项。
    如果stack为空,抛出异常EmptyStackException

  3. pop(): E
    Returns the element at the top of the stack and removes it.
    移除栈顶对象,并作为函数值返回。
    如果stack为空,抛出异常EmptyStackException

  4. push(E): E
    Pushes the specified object onto the top of the stack.
    把项压入栈顶,并返回该项

  5. search(Object): int
    Returns the index of the first occurrence of the object, starting from the top of the stack.
    返回对象在栈中的位置:如果该对象在栈顶则返回1,如果改对象在栈底则返回stack的size,如果栈中有多项相同的项则返回离栈顶最近的那项的位置。


另:实际运用中发现的问题如下(后期继续补充)

  • 使用add(E)和push(E)效果是一样的

注:笔者依据的是jdk1.8.0_45,与其他版本有出入属于正常现象

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容