java - Groovy how to push value to array of type long -


new groovy take easy... error there no push() method array of longs

def mylongs=[] long[]; somebject.each{      //logic chooose...      mylongs.push(it.thisisalong); } 

so how append long values properly. using

 mylongs[mylongs.size()]=it.thisisalong   

yields out of index bounds exceptions

first let me address second question, , larger difference between arrays , lists in jvm:

arrays, , lists in java 0-based, meaning first element can found in a[0], , last element, in a[a.size()-1]. element a[a.size()] exceeds bounds of array, , exception tells you.

in groovy can use a.last(), if want pick last element of array/list, in opinion more readable , self-explanatory.

if cast mylongs array before populating it, have fixed size of array, , can push no more objects it. if array has variable size, need use list.

list<long> a=[]  << 1 long << 2 long 

etc

when need convert array, can this:

a long[] 

now answer of first question, others pretty gave valid answer, in groovy style, i'd write (providing somebject collection of type):

def mylongs= somebject.collect{ it.thisisalong } long[] 

but pushing element list , done this, in groovy style:

mylongs << 4 

you cannot append values array, has fixed size.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -