• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayDeQueue data structure

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the book OCP_ Oracle Certified Professional Java SE 8 Programmer II Study Guide_ Exam
The authors provide a table containing some methods of ArrayDequeue :
MethodDescriptionFor queueFor stack
boolean add(E e)Adds an element to....YesNo
............

What I don't understand is this : for queue and for stack
I think that we could define an ArrayDequeue  with a stack or a queue as:
//1

however when I tried to use the 1st declaration the code did not compile
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yas,
Great question. ArrayDequeue implements Queue. (Stack is a legacy class)

In addition to the Queue and Stack interface/class, there are the concepts of a queue and a stack. So you can use an ArrayDequeue like a logical queue of people online or like a stack of dishes. And to do so, you call different methods.
 
yas sine
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for responding,
so to use stack methods we use :

and to use queue methods :

is that right ?
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java doesn't have a Stack interface that extends Collection, but if it had, the Deque interface still wouldn't be able to extend it, because either the add() or the remove() method would be ambiguous.

They could have added a Stack interface that doesn't extend Collection, and then let Deque extend both Stack and Queue, but they probably thought it wasn't worth it if you can't treat a Stack like a regular collection.
 
yas sine
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a remark (and correct me if I'm wrong) : the Interface Queue doesn't have a method push, therefor, a reference to ArrayDeque of type Queue (i.e ) will not be able to call push method as the book suggest.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right. push() doesn't make sense on queues. It's a stack operation.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yas sine wrote:Just a remark (and correct me if I'm wrong) : the Interface Queue doesn't have a method push, therefor, a reference to ArrayDeque of type Queue (i.e ) will not be able to call push method as the book suggest.


That is covered on page 136 - "Except for push, all are in the Queue interface as well. push is what makes it a double ended queue"
 
yas sine
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
right it is covered, but in the table  describing the ArrayDeque it is said that push works for Queue and Stack (yes in both columns )
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yas sine wrote:right it is covered, but in the table  describing the ArrayDeque it is said that push works for Queue and Stack (yes in both columns )


No. It says that the push() method is on both queue and stack. That is the *concept* of a queue and a stack. Not the Queue/Stack class/interface.
 
Grow a forest with seedballs and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic