Skip to content
Groovy 13 - Array

An array is a variable that can store multiple values of the same type.

In traditional Java, arrays cannot store different data types. An array should store only one type: Strings, Integers, Doubles, etc.

The classic style in Java:

        def array_name = DataType[n]
        

n is the number of elements the array will contain. Elements are added to the array like so:

        array_name[0] = Element_1
        array_name[1] = Element_2
        array_name[2] = Element_3
        ...
        array_name[n] = Element_n
        

Example

        def array_name = new String[3]
        array_name[0] = "Red"
        array_name[1] = "Yellow"
        array_name[2] = "Green"
        

Each element in the array is accessed via an index, starting from 0. The first element is at index 0.

Arrays in Sales Cloud

In Sales Cloud, you can define an array without specifying the number of elements:

        def array_name = []
        

This allows you to add as many elements as needed without being restricted by a predefined size.

Example

        def array_1 = Integer[2]
        // You must store exactly 2 elements
        array_1[0] = 1000
        array_1[1] = 2000
        // array_1[2] = 3000 // This will cause an ArrayIndexOutOfBoundsException
        

If you define an array without specifying the size, you can add elements dynamically:

        def array_2 = []
        array_2[0] = 1
        array_2[1] = 2
        array_2[2] = 3
        array_2[3] = "text"
        

The Groovy engine in Sales Cloud can handle mixed data types in an array, though it is not a good practice.

Another Example

        def array_3 = [4]
        array_3[0] = "1"
        array_3[1] = "2"
        array_3[2] = 1
        array_3[3] = 5
        // This restricts the array to 4 elements
        

Array Methods

The most notable methods are add() and size().

Using add()

        def fruits = []
        fruits.add("Apple")
        fruits.add("Tomato")
        fruits.add("Peach")
        

This can also be written as:

        fruits[0] = "Apple"
        fruits[1] = "Tomato"
        fruits[2] = "Peach"
        

Using size()

To get the size of the array:

        fruits.size() // Output: 3
        

Looping Through an Array

To loop through an array, we use a for loop.

Example

        def fruits = []
        fruits.add("Apple")
        fruits.add("Tomato")
        fruits.add("Peach")
        
        for (def i = 0; i < fruits.size(); i++) {
            println(fruits[i])
        }
        

Output:

        Apple
        Tomato
        Peach
        

Using for-each Loop

        def num = []
        num[0] = 1
        num[1] = 2
        num[2] = 1
        num[3] = 5
        
        for (def x : num) {
            println(x)
        }
        

Output:

        1
        2
        1
        5
        

Infinite Loop

An infinite loop is a cycle that never ends, causing an error like:

oracle.jbo.ExprTimeoutException Expression timed out.

Example of Infinite Loop

        def var_x = 80
        while (var_x) {
            println(var_x)
        }