Collection of strings - two different ways

P

Peppy

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?
 
H

Hendrik Maryns

-----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+7xMGD3itQRAki6AJ0Zec6XOjglu68xYYlwo2fktg9WAQCdGrbG
Mhnelht1MgLcC1rjwsRQNmg=
=b3Jm
-----END PGP SIGNATURE-----
 
R

Robert Klemme

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Peppy schreef:

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
 
L

Lew

Peppy schreef:
"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.
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Robert Klemme schreef:
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+K7Jc5gp9RYQ6dBScwCbBfnM
OSfL1nJj09w2N+lCkE13jZs=
=bVs4
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top