Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Collection of strings - two different ways

Reply
Thread Tools

Collection of strings - two different ways

 
 
Peppy
Guest
Posts: n/a
 
      05-24-2007
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?

 
Reply With Quote
 
 
 
 
Hendrik Maryns
Guest
Posts: n/a
 
      05-24-2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Peppy schreef:
> 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?


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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGVWxUe+7xMGD3itQRAki6AJ0Zec6XOjglu68xYYlwo2 fktg9WAQCdGrbG
Mhnelht1MgLcC1rjwsRQNmg=
=b3Jm
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      05-24-2007
On 24.05.2007 12:43, Hendrik Maryns wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Peppy schreef:
>> 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?

>
> 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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      05-24-2007
Peppy schreef:
>>> List <String> list = new ArrayList <String> ( );
>>> ArrayList <String> list = new ArrayList <String> ( );


Robert Klemme wrote:
>> 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.

--
Lew
 
Reply With Quote
 
Hendrik Maryns
Guest
Posts: n/a
 
      05-24-2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Robert Klemme schreef:
> On 24.05.2007 12:43, Hendrik Maryns wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Peppy schreef:
>>> 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?

>>
>> 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()).


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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGVbvse+7xMGD3itQRAr+aAJ44xPrE+xUS+K7Jc5gp9R YQ6dBScwCbBfnM
OSfL1nJj09w2N+lCkE13jZs=
=bVs4
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Different ways of declaring strings bintom C++ 6 10-06-2008 02:28 PM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
Two identical Strings stored in two different object Neroku Java 12 02-12-2007 03:32 PM
different ways to strip strings rtilley Python 6 02-27-2006 10:21 PM
Why do Java offers two different ways to write the multi-threaded Java programs? Rodney Edmondson Java 11 12-09-2003 03:46 AM



Advertisments