Skip to content
Groovy 17 – Multidimensional Array

Usually, Multidimensional Arrays are used to create a table. In Oracle Sales Cloud, there is not much need for them, but you can still create them in Groovy. Here is an example:

        def myArray = [[100, 200, 300, 400], [1, 2, 3, 4], [99999]]
        

This means that the array myArray has 3 dimensions, visually like 3 rows in a table.

Table

You can access the elements of the array using loops. The most common way is by using two for loops: the first loop cycles through the dimensions, and the second loop cycles through the items of each dimension. Here is an example in a formula field:

        def myArray = [[100, 200, 300, 400], [1, 2, 3, 4], [99999]]
        def result = []
        
        def row_1 = myArray[0].size()
        def row_2 = myArray[1].size() + row_1 + 1
        
        for (int row = 0; row < myArray.size(); row++) {
            for (int column = 0; column < myArray[row].size(); column++) {
                result.add(myArray[row][column] + "\t")
            }
        }
        
        result.add(row_1, "\n")
        result.add(row_2, "\n")
        
        def string_val = substringAfter(substringBefore(result.toString(), ']'), '[')
        def newString = string_val.replaceAll(',', '')
        
        return newString
        

Example Output 1

The output will be:

Example Output 2

Explanation:

        def myArray = [[100, 200, 300, 400], [1, 2, 3, 4], [99999]]
        

This is my multidimensional array with 3 dimensions.

        def result = []
        

This is a simple array to collect the output of the for loops.

        def row_1 = myArray[0].size()
        def row_2 = myArray[1].size() + row_1 + 1
        

Here, I calculate where the rows should be split.

        for (int row = 0; row < myArray.size(); row++) {
            for (int column = 0; column < myArray[row].size(); column++) {
                result.add(myArray[row][column] + "\t")
            }
        }
        

The first loop cycles through the dimensions. The second loop cycles through the items in each dimension. Each item is added to the result array with a tab for visibility.

        result.add(row_1, "\n")
        result.add(row_2, "\n")
        

Adding a newline at the calculated row splits for table formatting.

        def string_val = substringAfter(substringBefore(result.toString(), ']'), '[')
        

Removing the square brackets from the array string representation.

        def newString = string_val.replaceAll(',', '')
        

Creating a new string without commas (Strings are immutable).

        return newString
        

Displaying the result in the formula field.

How to Add Items to the Multidimensional Array:

You cannot use add() as multidimensional arrays are immutable. Use the following:

        myArray[0][0] = 1
        // This will introduce item 1 to dimension 1 at index 0
        

Or:

        myArray.get(2).set(0, 777777)
        // This will replace value 99999 with 777777 in dimension 3 at index 0
        

Java.lang.IndexOutOfBoundsException:

This error occurs when a non-existing index from an array is called. For example:

        myArray.get(2).set(1, 777777)
        // This will cause a java.lang.IndexOutOfBoundsException because dimension 3 has only one index (0).
        

I hope you found this useful. This example shows how Multidimensional Arrays work in Oracle Sales Cloud.