java - How do I write a method which would work on both lists and arrays? -


i have method looks this:

void foo (list<string> list, ...) {   ...   (string s : list) { // place `list` used     ...   }   ... } 

the exact same code work if replace list<string> list string[] list, however, avoid spaghetti code, keep single method, , when need call on array a, this: foo(arrays.aslist(a)).

i wonder if right way.

specifically,

  • what overhead of arrays.aslist()?
  • is there way write method accept both arrays , lists, for loop does?

thanks!

arrays.aslist() has small overhead. there no real way implement 1 method both list , arrays.

but can following:

void foo (list<string> list, ...) {   ...   (string s : list) { // place *list* used     ...   }   ... }  void foo (string[] arr, ...) {   if ( arr != null ) {       foo(arrays.aslist(arr),...);   } } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

jquery - Invalid Assignment Left-Hand Side -

gmail - Is there any documentation for read-only access to the Google Contacts API? -