Sunday, November 3, 2013

ArrayDeque

ArrayDeque is a data structure that implements the Deque interface. Deque is the short form of "double ended queue" and generally pronounced as "deck". The ArrayDeque does not limit the number of its elements. It can grow when needed. It is likely to be faster than stack or queue data structure when it used as stack or queue data structure. Most its methods (e.g. add, addFirst, addLast, clear, clone, getFirst, getLast, isEmpty, peekFirst, peakLas, pollFirst, pollLast, size, and toArray) of the ArrayDeque run in constant time (c) and few methods (e.g. remove, removeFirstOccurence, removeLastOccurrence, and contains) run in linear time (O(n)). You can create an ArrayDeque object by using one of the ArrayDeque constructors.

-ArrayDeque() creates an empty deque enough for storing 16 elements.
-ArrayDeque(Collection<? extends E> c) creates a deque from a specified collection object (e.g. ArrayDeque, LinkedList, Stack, ...).
-ArrayDeque(int capacity) creates an empty deque with initial capacity (number of elements).

To add a new element to the ArrayDeque object, you can use the add(E e), addFirst(E e), and addLast(E e) methods. The add and addLast method will add the element at the end of the deque. The addFirst method will add the element to the front of the deque.

Example:

ArrayDeque<Integer> ad=new ArrayDeque<Integer>();
Random random=new Random();
for(int i=0;i<10;i++){
   ad.add(1+Math.abs(random.nextInt())%6);
}

To remove elements from the ArrayDeque object, you will use the clear, poll, pollFirst, pollLast, remove, remove(Object o), removeFirst(), removeFirstOccurence(Object o), removeLast, and removeLastOccurrence (Object o). The clear method will remove all elements of the deque. The poll, pollFirst, remove, removeFirst methods will retrieve and remove the first element of the deque. The pollLast and removeLast method will retrieve and remove the last elelement of the deque. If you want to remove any element in the deque, you can use the remove(Object o) method. The removeFirstOccurrence will remove the the first matched element and the removeLastOccurrence will remove the last matched elements.

Example:

int fele=ad.remove();
System.out.println("Removed "+fele);
int lele=ad.removeLast();
System.out.println("Removed "+lele);

If you want to get the elements without removing them from the deque, you will use the getFirst, getLast, peekFirst, and peekLast methods. The getFirst and peekFirst methods return the first element of the deque. In contrast, the getLast and peekLast methods return the last element of the deque.

Example:

int fele=ad.getFirst();
System.out.println("The first element is "+fele+".");
int lele=ad.getLast();
System.out.println("The last element is "+lele+".");
Another useful method of the ArrayDeque is iterator(). The iterator method can be used traverse through the deque. See the example code below.

Iterator<Integer> i=ad.iterator();
while(i.hasNext()){
   System.out.print(i.next()+"\t");

}

6 comments:

  1. nice article in your blog.thank you for sharing useful info.
    visit
    web programming tutorial
    welookups

    ReplyDelete
  2. Hello there! I know this is kind of off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having difficulty finding one? Thanks a lot! Cultural Anthropology Cultural Psychology heine test bank solutions manual

    ReplyDelete
  3. I don’t even know how I ended up here, but I thought this post was great. I do not know who you are but certainly you are going to a famous blogger if you aren't already ;) Cheers! design your own website

    ReplyDelete
  4. you're really a good webmaster. The web site loading speed is amazing. It seems that you're doing any unique trick. Furthermore, The contents are masterpiece. you have done a great job on this topic!branded t shirts at low price

    ReplyDelete
  5. I happen to be commenting to let you be aware of what a brilliant discovery my wife's child went through studying yuor web blog. She discovered such a lot of things, which included what it is like to possess an incredible teaching mindset to get folks really easily fully grasp specific tricky subject areas. You undoubtedly surpassed readers' desires. I appreciate you for displaying such powerful, trustworthy, revealing and as well as unique tips on this topic to Mary. social media in Singapore

    ReplyDelete