The definition of an array and the initial value of an assignment
In the groovy language, arrays are defined in the same way as in the Java language.
def a = new String[4]
def nums = newint[10]
def objs = new Object[3]
And then the assignment is the same:
a[0] = 'a'
a[1] = 'b'
a[2] = 'c'
a[3] = 'd'
The difference is in assigning an initial value when the array is defined.
In the Java language, this is defined as an array of strings:
String[] strs = new String[]{'a','b','c','d'};
In the groovy language, it is necessary to define a string array:
def strs = ['a','b','c','d'] as String[]
Second, the array of traversal
In the groovy language, there are many ways to iterate an array, often using each method:
a.each{
println it
}
Of course, you can also use the enhanced for loop:
for(it in a)
{
println it
}
You can also use the following traversal method:
(0..<a.length).each{
println a[it]
}