This article has introduced the Java.util.ArrayDeque class use method for everybody, the concrete content as follows
1. Arraydeque has two class attributes, head and tail, two pointers.
2. Arraydeque through an array as the carrier, where the array elements in the add and other methods do not move, the change is only head and tail pointers, and the pointer is cyclic change, the array capacity is not limited.
3. The Offer method and add method are all implemented through the AddLast method, each adding an element to the end of the array, at which point the head pointer does not change, and the tail pointer adds one, because the pointer is circular plus, so when the tail catches up to it ( This.tail = this.tail + 1 & this.elements.length-1) = = This.head), the array capacity is doubled to continue execution.
4. The Remove method and the poll method are implemented through the Pollfirst method, where each element is removed, and the position of the element is null, at which point the tail pointer does not change, and the head pointer adds one and returns NULL when there is no data in the array.
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.ArrayDeque;
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 < i++) {
Data.push ("www.what21.com." + i + "");
}
Delete the first
data.removefirst ();
Obtain the first
System.out.println (Data.peekfirst ());
Added 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 the entire content of this article, I hope to help you learn.