Xtend: Add element to array in for-loop -


i trying simplest operation in xtend, don't know how. want add double value double[] array inside for-loop.

for example:

def do(elist<myobject> list) {     var double[] array = newdoublearrayofsize(list.size);     (i : 0 ..< list.size) {         array[i] = list.get(i).myvalue;     }     return array; } 

the forth line shows error, because can't use array[i] = ....

how implement in xtend? haven't found in user guide.

xtend has different ("list-like") syntax accessing array elements, see related documentation details:

retrieving , setting values of arrays done through extension methods get(int) , set(int, t) overloaded arrays , translated directly equivalent native java code myarray[int].

so code should be:

def method(elist<myobject> list) {     var double[] array = newdoublearrayofsize(list.size);     (i : 0 ..< list.size) {         array.set(i, list.get(i).myvalue);     }     return array; } 

you can further simplify method omitting semicolons , type declaration of array variable:

def method(elist<myobject> list) {     val array = newdoublearrayofsize(list.size)     (i : 0 ..< list.size) {         array.set(i, list.get(i).myvalue)     }     return array } 

another alternative write method in more functional style. if can replace elist list (or elist extends/implements list) write:

def double[] method(list<myobject> list) {     list.map[myvalue] } 

in case must explicitly declare return type double[] because otherwise inferred list<double>.

(just 1 more thing: collections preferred on arrays because more flexible , have more rich apis, , xtend has additional goodies collection literals.)


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -