Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Deque question

Thread view: 
toton - 26 Oct 2006 15:07 GMT
Hi,
Java ArrayDeque is an Deque implementation over an array. However if
the array is gull it expands it, otherwise wraps the head and tail.
That is what I want.
However as it is an implementation over an array, I expect an random
access like E get(index i) method. But such method is not present. Why?
It is present in ArrayList or any other random access container! Though
it gives an delete(index i) method, which causes it to shift some of
the elements!

can anyone give a little insight?

Thanks
abir
Thomas Hawtin - 26 Oct 2006 17:16 GMT
>  Java ArrayDeque is an Deque implementation over an array. However if
> the array is gull it expands it, otherwise wraps the head and tail.
[quoted text clipped - 4 lines]
> it gives an delete(index i) method, which causes it to shift some of
> the elements!

I can only guess that it is to make it easier to subclass. Personally, I
would have made it implement List (and RandomAccess, but not Cloneable).

Tom Hawtin
heysc0tt@gmail.com - 26 Oct 2006 18:08 GMT
>  Java ArrayDeque is an Deque implementation over an array. However if
> the array is gull it expands it, otherwise wraps the head and tail.
[quoted text clipped - 4 lines]
> it gives an delete(index i) method, which causes it to shift some of
> the elements!

It seems to me that this is line with the design of the interface.
Being a Queue at heart it seems logical to disallow index based
operations and try to restrict access to only head and tail elements.
hiwa - 27 Oct 2006 00:08 GMT
> Being a Queue at heart it seems logical to disallow index based
> operations and try to restrict access to only head and tail elements.
Quite right.
toton - 27 Oct 2006 06:56 GMT
> > Being a Queue at heart it seems logical to disallow index based
> > operations and try to restrict access to only head and tail elements.
> Quite right.
However it allows index based deletion! What is the problem with index
based access? Otherwise what is the necessity to implement it over an
Array? Surprisingly enough LinkedList , which is not backed by and
array, and implements Deque has an index based access! A Deque property
does not get lost giving index based access, it looses the property
when it allows index based insert and remove (which it has already
done!)

Thanks
Patricia Shanahan - 27 Oct 2006 11:28 GMT
>>> Being a Queue at heart it seems logical to disallow index based
>>> operations and try to restrict access to only head and tail elements.
[quoted text clipped - 8 lines]
>
> Thanks

Isn't the index based delete method private?
http://download.java.net/jdk6/doc/api/java/util/ArrayDeque.html does not
mention it.

Patricia
Chris Uppal - 27 Oct 2006 10:15 GMT
>  However as it is an implementation over an array, I expect an random
> access like E get(index i) method. But such method is not present. Why?

I Assume you're talking about the ArrayDeque in JDK1.6 ?

If so then I can't see any good reason.  It seems daft to me.

Looking at the code, it seems that the only reason for that omission (and also
the only reason for the equally odd restriction that the inserted values cannot
be null) is to simplify the code a little and reduce the amount of arithmetic
bounds checking needed.  If so then (IMO) that/those are very poor reasons, and
not at all what I would expect when:

   @author  Josh Bloch and Doug Lea

Note that the iterator /does/ make use (internally) of indexed access...

Still, 1.6 is beta software, so maybe this'll change before release.

   -- chris
Thomas Hawtin - 27 Oct 2006 14:46 GMT
> Looking at the code, it seems that the only reason for that omission (and also
> the only reason for the equally odd restriction that the inserted values cannot
[quoted text clipped - 3 lines]
>
>     @author  Josh Bloch and Doug Lea

As a rule, collection classes written by Josh Bloch permit nulls, those
written by Doug Lea do not.

> Note that the iterator /does/ make use (internally) of indexed access...

I would hope so. Note, the iterator does support remove...

Tom Hawtin
toton - 28 Oct 2006 08:24 GMT
> >  However as it is an implementation over an array, I expect an random
> > access like E get(index i) method. But such method is not present. Why?
>
> I Assume you're talking about the ArrayDeque in JDK1.6 ?
Yes, ArrayDeque in JDK1.6 beta . This class is a very important
addition (esp for me!, and may be a very strong reason why I am using
1.6, apart from Those taskbar icons etc).
> If so then I can't see any good reason.  It seems daft to me.
But it is beta version. I don't think API will change after a beta
release.
> Looking at the code, it seems that the only reason for that omission (and also
> the only reason for the equally odd restriction that the inserted values cannot
[quoted text clipped - 3 lines]
>
>     @author  Josh Bloch and Doug Lea
Is he same person as  Joshua Bloc of effective Java? Then I have a
reason! He was in hurry while leaving Sun (and joining Google) :) I
have an old code by them also, a non-generic version.
> Note that the iterator /does/ make use (internally) of indexed access...
Yes, everything uses indexed access, and that is natural when it is
implemented over an array!
And oh! The delete(int index ) is private, and have a special use in
removeLastOccurrence and removeFirstOccurrence. It shifts the
references using arraycopy.
> Still, 1.6 is beta software, so maybe this'll change before release.
Hope so!
Thanks
>     -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.