This article introduces the usage method of java.util.ArrayDeque for your reference. The specific content is as follows
1. ArrayDeque has two class attributes, head and tail, and two pointers.
2. ArrayDeque uses an array as a carrier, and the array elements do not move when executing methods such as add. The only changes are head and tail pointers, and the pointers are loop changes, and the array capacity is not limited.
3. The offer method and the add method are both implemented through the addLast method. Every time an element is added, the element is added to the tail of the array. At this time, the head pointer does not change, and the tail pointer is added one. Because the pointer is added loop, when tail catches up with the head ((this.tail = this.tail + 1 & this.elements.length - 1) == this.head), the array capacity doubles and continues to be executed.
4. The remove method and poll method are both implemented through the pollFirst method. Every time an element is removed, the position of the element becomes null. At this time, the tail pointer does not change, and the head pointer is added one. When there is no data in the array, null is returned.
5. Because ArrayDeque is not thread-safe, it is faster than Stack when used as a stack and faster than LinkedList when used as a queue.
package com.what21.collect11; import java.util.ArrayDequ;import java.util.Deque; public class ArrayDequeDemo { /** * @param args */ public static void main(String[] args) { Deque<Object> data = new ArrayDeque<Object>(); // Add element for (int i = 0; i < 20; i++) { data.push("www.what21.com ." + i + " "); } // Delete the first data.removeFirst(); // Get the first System.out.println(data.peekFirst()); // Add to the last data.addLast("www.what21.com .9999"); // System.out.println(data); // traversal for(Object o : data){ System.out.println(o); } } }The above is all about this article, I hope it will be helpful to everyone's learning.