• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections as arguments

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a method that draws a polygon on the screen:



For the usage of the method, it makes no difference whether points is an array or a List. In fact, switching doesn't even change any of the code in the method since I use a for-each loop. So, my question is, is it a better practice to use a List as an argument, or an array when the method itself doesn't care about which to use?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jack Snipes wrote:So, my question is, is it a better practice to use a List as an argument, or an array when the method itself doesn't care about which to use?


My opinion: A List - or possibly even a Collection (the general rule being: "the vaguer the better" ).

Arrays are a very specific animal: For one thing they have a fixed size; Lists don't. They can also be a PITA when they involve types.

For example, you can't create an array of Comparators, eg:
Comparator<String>[] comparators;

without an explicit cast, viz:
Comparator<String>[] comparators = (Comparator<String>[]) new Comparator[10];

and even then I think you get a compiler warning.

You can certainly create a List of them though:
List<Comparator<String>> comparators = new ArrayList<>();

HIH

Winston
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:My opinion: A List - or possibly even a Collection (the general rule being: "the vaguer the better" ).


The vaguer the better?
So let's go crazy and use Iterable.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:The vaguer the better?
So let's go crazy and use Iterable.


Good point. Lets.

Winston
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing crazy about it. If you specifically want it to iterate over, then Iterable<Foo> is the thing to use.
reply
    Bookmark Topic Watch Topic
  • New Topic