public class ArrayTest{
public static void main(String[] args){
String[] list = {"key1", "key2", "key3"};
String[] list2 = {"val1", "val2", "val3"};
for(int i = 0; i < list.length; i++){
ilike(list[i], list2[i];
}
}
}
How to write the above code in Groovy?
Actually, its a grails application where I want to do similar thing above.
You have a couple of options that come to mind...
Given:
String[] list = [ 'key1', 'key2', 'key3' ]
String[] list2 = [ 'val1', 'val2', 'val3' ]
Then you could do:
list.eachWithIndex { a, i ->
ilike a, list2[ i ]
}
or assuming ilike is defined as:
void ilike( String a, String b ) {
println "I like $a and $b"
}
Then you can do (using transpose
):
[list,list2].transpose().each {
ilike it
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With