JDK1.8源码-10-java.util.Stack

JDK1.8源码-10-java.util.Stack

前序:之前我们学完了Vector,接下来开始学习Stack。Stack很简单,它继承了Vector。

1. 简介

  • Stack叫做栈
  • 特性是:先进后出(FILO:First in Last Out)
  • java工具包中的Stack继承于Vecotr(矢量队列)
  • 由于Vector是通过数组实现的,这就意味着Stack也是通过数组实现的。
  • 当然LinkedList也可以当做栈使用。

mark

由于Stack和继承于Vector,因此它也包含Vector中的全部API

2. 构造函数

  1. 只有一个构造函数
1
2
3
4
5
6
7
public
class Stack<E> extends Vector<E> {
/**
* Creates an empty Stack.
*/
public Stack() {
}

3. push

  • push函数:将元素存入栈顶
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19

    ```java
    /**
    * Pushes an item onto the top of this stack. This has exactly
    * the same effect as:
    * <blockquote><pre>
    * addElement(item)</pre></blockquote>
    *
    * @param item the item to be pushed onto this stack.
    * @return the <code>item</code> argument.
    * @see java.util.Vector#addElement
    */
    public E push(E item) {
    // 调用Vector的addElement方法
    // 将元素存入栈顶
    addElement(item);

    return item;
    }

4. peek

  •  peek函数:返回栈顶元素,不执行删除操作
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    ```java
    /**
    * Looks at the object at the top of this stack without removing it
    * from the stack.
    *
    * @return the object at the top of this stack (the last item
    * of the <tt>Vector</tt> object).
    * @throws EmptyStackException if this stack is empty.
    */
    public synchronized E peek() {
    int len = size();

    if (len == 0)
    throw new EmptyStackException();
    // 返回栈顶元素
    return elementAt(len - 1);
    }

5. pop

  • pop函数:返回栈顶元素,并将其从栈中删除
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23



    ```java
    /**
    * 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 <tt>Vector</tt> object).
    * @throws EmptyStackException if this stack is empty.
    */
    public synchronized E pop() {
    E obj;
    int len = size();

    // 获取栈顶元素
    obj = peek();
    // 调用Vector的删除方法:将栈顶元素删除
    removeElementAt(len - 1);

    return obj;
    }

6. empty

  • 判断栈是否为空
1
2
3
4
5
6
7
8
9
/**
* Tests if this stack is empty.
*
* @return <code>true</code> if and only if this stack contains
* no items; <code>false</code> otherwise.
*/
public boolean empty() {
return size() == 0;
}
  • // 查找“元素o”在栈中的位置:由栈底向栈顶方向数
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25

    ```java
    /**
    * Returns the 1-based position where an object is on this stack.
    * If the object <tt>o</tt> occurs as an item in this stack, this
    * method returns the distance from the top of the stack of the
    * occurrence nearest the top of the stack; the topmost item on the
    * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
    * method is used to compare <tt>o</tt> to the
    * items in this stack.
    *
    * @param o the desired object.
    * @return the 1-based position from the top of the stack where
    * the object is located; the return value <code>-1</code>
    * indicates that the object is not on the stack.
    */
    public synchronized int search(Object o) {
    // 从后往前获取元素第一次出现的索引
    int i = lastIndexOf(o);

    if (i >= 0) {
    return size() - i;
    }
    return -1;
    }

8. 总结

  • Stack其实也是通过数组实现的
    • 执行push方法的时候,是将元素追加在数组的末尾
    • 执行peek方法的时候,是返回数组末尾的元素(取出栈顶元素,不执行删除)
    • 执行pop方法的时候,是返回数组末尾的元素同时删除末尾的元素(取出栈顶元素并删除)
  • Stack继承于Vector,可以使用Vector的所有API方法。

9. Stack 演示用例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public static void main(String[] args) {
Stack stack = new Stack<>();

// 1. 入栈
for (int i = 0; i < 6; i++) {
stack.push(String.valueOf(i));
}


// 2. for遍历打印栈
RandomGet(stack);

// 3. 查找位置
int pos = stack.search("2");
System.out.println("the position is "+ pos);

// 4. pop元素之后再打印栈
stack.pop();
IteratorStack(stack);

// 5. peek元素之后再打印栈
stack.peek();
IteratorStack(stack);

// 6. 通过iterator打印栈
IteratorStack(stack);


}

// for循环打印
public static void RandomGet(List list){
String val = null;
for (int i = 0; i < list.size(); i++) {
val = (String) list.get(i);
System.out.println(val);
}
System.out.println();
}

// 迭代器打印
public static void IteratorStack(List list){
String val = null;
for (Iterator it = list.iterator();it.hasNext();){
val = (String)it.next();
System.out.println(val);
}
System.out.println();
}
打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2019-2022 Zhuuu
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信