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 / May 2007

Tip: Looking for answers? Try searching our database.

Collection of strings - two different ways

Thread view: 
Peppy - 24 May 2007 11:32 GMT
Hi,

I was looking at creating a collection of strings and I've seen two
different means of doing this

List <String> list = new ArrayList <String> ( );
ArrayList <String> list = new ArrayList <String> ( );

which one should I use?
Hendrik Maryns - 24 May 2007 11:43 GMT
Peppy schreef:
> Hi,
>
[quoted text clipped - 5 lines]
>
> which one should I use?

The first.  Google for ‘prefer interface over implementation’ or some
rule in that line.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Robert Klemme - 24 May 2007 12:14 GMT
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 12 lines]
> The first.  Google for ‘prefer interface over implementation’ or some
> rule in that line.

Exception: you need some methods specific to the class (for example
LinkedLists's removeFirst()).

    robert
Lew - 24 May 2007 13:16 GMT
Peppy schreef:
>>> List <String> list = new ArrayList <String> ( );
>>> ArrayList <String> list = new ArrayList <String> ( );

>> The first.  Google for ‘prefer interface over implementation’ or some
>> rule in that line.

"Use the most general type appropriate to your algorithm."

If List is too general, use the second, otherwise prefer the first as Robert said.

One advantage of the first is that you can very easily refactor the
declaration, e.g.,
 List <String> stuff = new TreeList <String> ();

Side note: "list" is a terrible name for a variable, especially if its type is
not "List", as in the second case.  Either way it says little to nothing about
the interpretation of the variable.

Signature

Lew

Hendrik Maryns - 24 May 2007 17:23 GMT
Robert Klemme schreef:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
[quoted text clipped - 15 lines]
> Exception: you need some methods specific to the class (for example
> LinkedLists's removeFirst()).

Well, no, then you probably want to declare the variable of the type
Deque.  LinkedList implements a lot of interfaces.  I have used it as a
Queue as well.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


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.