In J2SE6, the ArrayDeque class was introduced, which inherits the Deque (bidirectional queue) interface. Using this class, you can implement the functions of the java.util.Stack class by yourself, and removes the multi-threaded synchronization function of java.util.Stack.
For example, create a Stack that stores Integer type, just create a variable of the ArrayDeque class as a property in the class, and then define it as a stack and enter the stack, and observe the elements on the top of the stack, and directly operate the instance variable of the ArrayDeque.
import java.util.ArrayDequ; import java.util.Dequ; public class IntegerStack { private Deque<Integer> data = new ArrayDequ<Integer>(); public void push(Integer element) { data.addFirst(element); } public Integer pop() { return data.removeFirst(); } public Integer peek() { return data.peekFirst(); } public String toString() { return data.toString(); } public static void main(String[] args) { IntegerStack stack = new IntegerStack(); for (int i = 0; i < 5; i++) { stack.push(i); } System.out.println(stack); System.out.println("After pushing 5 elements: " + stack); int m = stack.pop(); System.out.println("Popped element = " + m); System.out.println("After popping 1 element: " + stack); int n = stack.peek(); System.out.println("Peeked element = " + n); System.out.println("After peeking 1 element : " + stack); } } Source code of java.util.ArrayDeque:
private transient E[] elements; private transient int head; private transient int tail; /*The location where e is stored here is stored from the last position of the elements array*/ public void addFirst(E e) { if (e == null) throw new NullPointerException(); elements[head = (head - 1) & (elements.length - 1)] = e;//The default for the first array capacity is 16, head=(0-1)&(16-1)=15 if (head == tail) doubleCapacity(); } /*Every time the capacity is expanded, it is put back into a new array in the order of insertion, and the latest inserted one is placed at the first position of the array. */ private void doubleCapacity() { assert head == tail; int p = head; int n = elements.length; int r = n - p; // number of elements to the right of p int newCapacity = n << 1; if (newCapacity < 0) throw new IllegalStateException("Sorry, deque too big"); Object[] a = new Object[newCapacity]; System.arraycopy(elements, p, a, 0, r); System.arraycopy(elements, 0, a, r, p); elements = (E[])a; head = 0; tail = n; } public E removeFirst() { E x = pollFirst(); if (x == null) throw new NoSuchElementException(); return x; } public E pollFirst() { int h = head; E result = elements[h]; // Element is null if deque empty if (result == null) return null; elements[h] = null; // Reset this position in the array to null for convenient garbage collection. head = (h + 1) & (elements.length - 1);//Reverse the value of head, which is equivalent to moving the pointer of the stack down one grid. For example, 12--〉13 return result; } public E peekFirst() { return elements[head]; // elements[head] is null if deque empty }The above is all about this article, I hope it will be helpful for everyone to learn Java programming.