Java Basics - Collections Framework --list the Stack class

A stack (Stack):. A data structure stored characteristics:. Last the In First Out
Stack class represents a last in first out (LIFO) stack of objects.
Stack structure embodied in life:
. 1): A message QQ,. B, C three people have sent messages, we found that the most viewed on top of the latest news.
2): pistol cartridges mounted and launch:
to be implemented stack storage array can be the underlying storage, you can also use chain stores.
Here Insert Picture Description

Second Operation FIG model
Here Insert Picture Description
Third Source:
Here Insert Picture Description

Four commonly used method
because all of the elements of the stack operation, the peek () method and pop () method is no parameters
Here Insert Picture Description

 		Stack stack = new Stack();
        stack.push("A");
        stack.push("B");
        stack.push("C");
        System.out.println(stack);//[A, B, C]
        System.out.println(stack.peek());//C
        stack.pop();
        System.out.println(stack);//[A, B]
        System.out.println(stack.peek());//B

Five .Deque
deque also be used as LIFO (last in, first out) stack. Priority should use this interface instead of the legacy Stack class. When the deque as stack elements is pushed into the beginning of the deque and ejected from the beginning of the deque. Stack method is exactly equivalent to Deque methods
official recommendations: use the stack to make use of ArrayDeque :

Deque interface and its implementation provides a more complete LIFO stack operations and more consistent set, priority should be given to use this set, but not in this category. E.g:

Deque stack = new ArrayDeque();

The first element to operate as a top

 ArrayDeque arrayDeque = new ArrayDeque();
        arrayDeque.push("A");
        arrayDeque.push("B");
        arrayDeque.push("C");
        System.out.println(arrayDeque);//[C, B, A]
        System.out.println(arrayDeque.peek());//C
        arrayDeque.pop();
        System.out.println(arrayDeque);//[B, A]
        System.out.println(arrayDeque.peek());//B
Published 99 original articles · won praise 2 · Views 2609

Guess you like

Origin blog.csdn.net/weixin_41588751/article/details/105255237