Skip to content

Groovy 13 – Array

An array is basically a variable that can store more than one value.

Also arrays can not store different data types, so you can not have an array that stores Strings and Integers.

The array should either store  only Strings or  only Integers or only Double so on…

This is the JAVA way, but we will see what we can do in Sales Cloud.

Lets say the classic style is as follows:

def array_name = DataType [n]

n– is the number of the elements that will be contained by the Array

and items 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


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

To be mentioned that each member of the array is called via an index.

An Array starts its index at 0, so first element/item of the array will be at index 0.

Looking above to the example,  you can see the first element of the array is “Red” and its stored at index 0.

In Sales Cloud you can define an array like so:

def array_name = [ ]

– now if you do it like this, you do not need to specify the number of elements  that will be contained by your array.


The difference between the first style and this second one is that, if you define an array be specifying form the start the number of elements that the array will contain, then you are pretty much restricted to use only that number of elements and of the same type

As if you use the second style, then you can add as many elements as you need without worrying that you are over the limit of elements.

Lets take an example.

If you do :  

def array_1 = Integer [2]  
//Then you absolutely have to store only 2 elements ion this array.  
So you have to do:  
array_1[0] = 1000   
array_1[1] = 2000

If you try to do array_1[2] = 2000  Then you will get a  a very nasty error like so: java.lang.ArrayIndexOutOfBoundsException

Which means your code is trying to call on a array index that does not exit.

But if you declare an array like so :

def array_2 =  [ ]

Now you an put anything in this array and as many elements as you need and of any type you want.

So you can do:  
array_2[0] = 1  
array_2 [1] = 2  
array_2 [2] = 3  
array_2 [3] = “text”

and if you would do operations with the array elements, the groovy engine in sales cloud is smart enough to know that if you would to do:

array_2[0]  + array_2[2]  the output will be 4

but if you do array_2[0] +array_2[3]  the output will be 1text

So it knows when to treat the elements as numeric type and when to treat as string.

But mixing out data ion array is not quite a good practice.

Now you can also define an array in Sales Cloud like so :

def array_3= [4]
array_3[0] = “1”
array_3[1] = “2”
array_3[2] = 1
array_3[3] = 5

If you do it like this it will restrict you to use only 4 elements on the array.

What methods can be used on Array.

Most notable and useful in Sales Cloud is add() method and size().

With add() you can add elements to your array like so:

def fruits = []

What the above mean is that fruits with index 0 stores the string “Apple” , at index 1 is “Tomato” and at index 2 is “Peach”

so this could also be written as:

fruits[0] = “Apple”
fruits[1] = “Tomato”
fruits[2] = “Peach”

With size() method you can find out how big is your array.

To keep with the example above doing fruits.size() with have an out put of 3.

How to cycle through an array?

Well to go through an array we will use loops, specifically for loop.

So we have the following array:

def fruits = [] 
for(def i = 0 ; i<fruits.size();i++){ 

Notice we can use add() to add elements to the array and size() to control the loop execution.

The output of the script above will be:

  1. Apple  
  2. Tomato  
  3. Peach

We can also use that special type of for each loop to move through the array:

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

the output of this will be:





Leave a Reply

Your email address will not be published. Required fields are marked *