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,
forloop 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
Post a Comment