Collection of strings - two different ways

Discussion in 'Java' started by Peppy, May 24, 2007.

  1. Peppy

    Peppy Guest

    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?
    Peppy, May 24, 2007
    #1
    1. Advertising

  2. -----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-----
    Hendrik Maryns, May 24, 2007
    #2
    1. Advertising

  3. 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
    Robert Klemme, May 24, 2007
    #3
  4. Peppy

    Lew Guest

    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
    Lew, May 24, 2007
    #4
  5. -----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+K7Jc5gp9RYQ6dBScwCbBfnM
    OSfL1nJj09w2N+lCkE13jZs=
    =bVs4
    -----END PGP SIGNATURE-----
    Hendrik Maryns, May 24, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rodney Edmondson
    Replies:
    11
    Views:
    654
    Jeremy
    Dec 9, 2003
  2. Todd MacCulloch

    different ways to creating two lists from one

    Todd MacCulloch, Oct 29, 2003, in forum: Python
    Replies:
    3
    Views:
    283
    Fredrik Lundh
    Oct 29, 2003
  3. rtilley

    different ways to strip strings

    rtilley, Feb 27, 2006, in forum: Python
    Replies:
    6
    Views:
    505
    Steven D'Aprano
    Feb 27, 2006
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    966
    Øyvind Isaksen
    May 18, 2007
  5. bintom
    Replies:
    6
    Views:
    397
    Gennaro Prota
    Oct 6, 2008
Loading...

Share This Page