• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections as arguments  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!