Groovy - Range toArray() method
toArray() method returns the array of the elements of the range.
Syntax
List toArray()
Parameters
NA
Return Value
Returns the list of elements of range values.
Example - Getting an array from a range of numbers
Following is an example of the usage of toArray() method.
Example.groovy
class Example {
static void main(String[] args) {
// range of numbers
def array = 1..10;
println(array.toArray());
}
}
Output
When we run the above program, we will get the following result −
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example - Getting an array from a range of characters
Following is an example of the usage of toArray() method.
Example.groovy
class Example {
static void main(String[] args) {
// range of characters
def array = 'a'..'z';
println(array.toArray());
}
}
Output
When we run the above program, we will get the following result −
[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]
Example - Getting array from a range of special characters
Following is an example of the usage of toArray() method.
Example.groovy
class Example {
static void main(String[] args) {
// range of special characters
def array = '&'..'!';
println(array.toArray());
}
}
Output
When we run the above program, we will get the following result −
[&, %, $, #, ", !]
groovy_ranges.htm
Advertisements