Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Range size() method



size() method returns the number of elements of this Range.

Syntax

int size()

Parameters

NA

Return Value

Returns the size of the range.

Example - Getting size of range of numbers

Following is an example of the usage of size() method.

Example.groovy

class Example { 
   static void main(String[] args) { 
      // range of numbers
      def array = 1..10;		
      println(array.size());     
   } 
}

Output

When we run the above program, we will get the following result −

10

Example - Getting size of range of characters

Following is an example of the usage of size() method.

Example.groovy

class Example { 
   static void main(String[] args) { 
      // range of characters
      def array = 'a'..'z'; 
      println(array.size());
   } 
}

Output

When we run the above program, we will get the following result −

26

Example - Getting size of range of special characters

Following is an example of the usage of size() method.

Example.groovy

class Example { 
   static void main(String[] args) { 
      // range of special characters
      def array = '&'..'!'; 
      println(array.size());     
   } 
}

Output

When we run the above program, we will get the following result −

6
groovy_ranges.htm
Advertisements