Java ArrayDeque implements Stack

  • 2020-05-09 18:31:53
  • OfStack

ArrayDeque class is introduced in J2SE6, which inherits the Deque(bidirectional queue) interface. With this class, the functions of java.util.Stack class can be realized by itself, and the multithreaded synchronization function of java.util.Stack is removed.

For example, if you want to create an Stack to store Integer type, just create a variable of ArrayDeque class as an attribute in the class. After that, you can directly manipulate the instance variable of ArrayDeque by observing the operation of the top element of the stack.  


import java.util.ArrayDeque; 
import java.util.Deque; 
 
public class IntegerStack { 
  private Deque<Integer> data = new ArrayDeque<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); 
  } 
}  

java.util.ArrayDeque source:        


private transient E[] elements; 
 private transient int head; 
 private transient int tail; 
 
/* The deposit e Is located from elements The position at the end of the array is stored */ 
 public void addFirst(E e) { 
    if (e == null) 
      throw new NullPointerException(); 
    elements[head = (head - 1) & (elements.length - 1)] = e;// The first array capacity defaults to 16 . head= ( 0-1 ) &(16-1)=15 
    if (head == tail) 
      doubleCapacity(); 
  } 
 
/* Each expansion is reinserted in the order in which it was inserted 1 The most recently inserted array is placed at the bottom of the array 1 A location. */ 
  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 the position in the array to null To facilitate garbage collection.  
    head = (h + 1) & (elements.length - 1);// will head Is the same as moving the stack pointer down again 1 Lattice. For example, 12-- > 13 
    return result; 
  } 
 
  public E peekFirst() { 
    return elements[head]; // elements[head] is null if deque empty 
  } 

The above is the entire content of this article, I hope to help you learn java programming.


Related articles: