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");

}

20 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
  6. 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.

    https://mulazmut.com/kp-government-jobs/

    ReplyDelete
  7. F*ckin’ tremendous things here. I’m very glad to see your article. Thanks a lot and i'm looking forward to contact you. Will you kindly drop me a mail? Copper Plate

    ReplyDelete
  8. If you are looking for latest nts news and jobs please check out https://ntsjobslatest.com

    ReplyDelete
  9. Please let me know if you're looking for a writer for your weblog. You have some really good posts and I believe I would be a good asset. If you ever want to take some of the load off, I'd love to write some material for your blog in exchange for a link back to mine. Please shoot me an e-mail if interested. Regards! pest control services

    ReplyDelete
  10. Please let me know if you're looking for a article author for your weblog. You have some really great articles and I think I would be a good asset. Rosacea treatment Singapore

    ReplyDelete
  11. Hi there! Someone in my Myspace group shared this site with us so I came to look it over. I'm definitely loving the information. I'm bookmarking and will be tweeting this to my followers! Great blog and brilliant design.industry 4.0

    ReplyDelete
  12. Everyone loves what you guys tend to be up too. This type of clever work and coverage! Keep up the awesome works guys I've incorporated you guys to my blogroll. outsourced accounting services singapore

    ReplyDelete
  13. We stumbled over here from a different page and thought I should check things out. I like what I see so now i'm following you. Look forward to looking over your web page repeatedly. sti index

    ReplyDelete
  14. Thank you for sharing superb informations. Your site is so cool. I'm impressed by the details that you’ve on this site. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for more articles. You, my pal, ROCK! I found just the information I already searched everywhere and simply couldn't come across. What an ideal web-site.nus executive education

    ReplyDelete
  15. Spot on with this write-up, I really assume this website needs far more consideration. I’ll in all probability be once more to read way more, thanks for that info.brand strategy

    ReplyDelete
  16. I'm really loving the theme/design of your blog. Do you ever run into any web browser compatibility problems? A few of my blog visitors have complained about my blog not operating correctly in Explorer but looks great in Chrome. Do you have any solutions to help fix this problem?diary singapore

    ReplyDelete
  17. I just like the valuable information you supply for your articles. I will bookmark your weblog and take a look at once more here regularly. I'm quite certain I’ll be informed lots of new stuff right right here! Best of luck for the next!Electrical Transformers

    ReplyDelete
  18. Hi there just wanted to give you a quick heads up. The words in your post seem to be running off the screen in Ie. I'm not sure if this is a formatting issue or something to do with browser compatibility but I figured I'd post to let you know. The design look great though! Hope you get the issue fixed soon. Many thankschinatown money lender

    ReplyDelete
  19. great post , someone shared this post with us on my discord and between us I'm bookmarking and will be tweeting this to my followers! Great blog and brilliant design Solution manual for Fundamentals of Corporate Finance 10th edition by Stephen Ross

    ReplyDelete