7.2 Other Extensions

The GDK extensions go beyond the ​Object​ class. Several other JDK classes and interfaces have been enhanced in the GDK. Again, the list is vast and we’ll look at only a subset of extensions in this section. These are the extensions we’re likely to put to regular use.

Array Extensions

We can use the ​Range​ object as an index on all the array types (for the syntax for creating arrays, see Different Syntax for Creating Primitive Arrays), such as ​int[]​, ​double[]​, and ​char[]​. Here’s how we can access contiguous values in an ​int​ array using the range of index:

ExploringGDK/Array.groovy
 
int​​[]​ arr = [1, 2, 3, 4, 5, 6]
 
 
println arr[2..4]

The output shows the values in the given range:

 
[3, 4, 5]

We’re already ...

Get Programming Groovy 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.