java.util.ArrayDeque class

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

This paper introduces the use of java.util.ArrayDeque class, for your reference, the specific content is as follows

1. ArrayDeque has two class properties, head and tail, and two Pointers.
2. ArrayDeque USES an array as a carrier, in which the array elements do not move when add and other methods are executed, only the head and tail Pointers change, and the Pointers change in a loop, so the array capacity is not limited.
3. offer method and add method are implemented by addLast method. Every time you add an element, you add the element to the end of the array. Double the size of the array and continue.
4. Both remove method and poll method are implemented by pollFirst method. For each element removed, the location of the element becomes null.
5. Because ArrayDeque is not thread-safe, it is faster as a stack than Stack, and faster as a queue than LinkedList.


package com.what21.collect11;
 
import java.util.ArrayDeque;
import java.util.Deque;
 
public class ArrayDequeDemo {
 
  /**
   * @param args
   */
  public static void main(String[] args) {
    Deque<Object> data = new ArrayDeque<Object>();
    //  Add elements 
    for (int i = 0; i < 20; i++) {
      data.push("www.what21.com ." + i + " ");
    }
    //  Delete the first 1 a 
    data.removeFirst();
    //  For the first 1 a 
    System.out.println(data.peekFirst());
    //  To the end 
    data.addLast("www.what21.com .9999");
    //
    System.out.println(data);
    //  traverse 
    for(Object o : data){
      System.out.println(o);
    }
  }
   
}
 

The above is the entire content of this article, I hope to help you with your study.


Related articles: