#set($array=[1,'house', 99.33])
$array
$array[0]
$array[1]
$array[2]
[1, house, 99.33]
1
house
99.33
#set($int=1) #set($string='house') #set($dec=99.33)
#set($array=[$int, $string, $dec])
$array
$array[0]
$array[1]
$array[2]
[1, house, 99.33]
1
house
99.33
#set($array=[['A', 'B'], ['C', 'D'])
$array[0][0]
$array[0][1]
$array[1][0]
A
B
C
#set($array1=['A', 'B']) #set($array2=['C', 'D'])
#set($array=[$array1, $array2])
$array[0][0]
$array[0][1]
$array[1][0]
A
B
C
#set($array1=['A', 'B']) #set($array2=[$array1, 'D'])
#set($array=[$array1, $array2])
$array[0][0]
$array[0][1]
$array[1][0][1]
A
B
B
#set($array1=[1..10]) #set($array2=[19..12]) #set($array3=[-3..5])
$array1
$array2
$array3
$array1[3]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[19, 18, 17, 16, 15, 14, 13, 12]
[-3, -2, -1, 0, 1, 2, 3, 4, 5]
4
Using the #foreach directive to loop over an array.
#set($array=[1..5])
#foreach($i in $array) $i #end
1
2
3
4
5
Using the #foreach directive to loop over a range of integers and dynamically add elements to an array.
#set($array=[])
#foreach($i in [1..5]) $array.add($i) #end
$array
[1, 2, 3, 4, 5]
Using the .size() function to count and return the number of elements in an array.
#set($array=[2,40,367])
$array.size()
3
Using the .isEmpty() function to check whether arrays are empty.
#set($array1=[2,40,367])
#set($array2=[])
$array1.isEmpty()
$array2.isEmpty()
false
true
Using the .clear() function to clear an array.
#set($array=[2,40,367])
$array
$array.clear()
$array
[2,40,367]
[]
Using the .add() function to append an element at the end of an array.
#set($array=[0,1,2])
$array
$array.add(3)
$array
[0,1,2]
[0,1,2,3]
Using the .add() function to add an element to a specific position in an array. The syntax is
.add(<index>,<value>).
#set($array=[2,4,8])
$array
$array.add(2,6)
$array
[2,4,8]
[2,4,6,8]
Using the .remove() function to remove an element from an array. The syntax is
.remove(<index>).
#set($array=[2,4,6])
$array
$array.remove(1)
$array
[2,4,6]
[2,6]
Using the type() function to check the types of the array elements.
#set($var=20)
#set($array=[10, 'a', 99.99, [1,2], $var])
$array
$array[0].type()
$array[1].type()
$array[2].type()
$array[3].type()
$array[4].type()
[10, a, 99.99, [1, 2], 20]
int
string
double
array
int
Using the toInt() function to convert an array of double values to an array of int values. The
#foreach directive is used to loop over the array, the
.remove() function is used to remove the original double value and the
.add() function is used to add the new int value.
#set($array=[10.6, 0.2, 99.99, 11.32, 20.55])
Original array: $array
#set($size=$array.size())
#foreach($i in [1..$size])
#set($index=$i-1)
#set($int=$array[$index].toInt())
$array.remove($index)
$array.add($index,$int)
#end
Modified array: $array
Original array: [10.6, 0.2, 99.99, 11.32, 20.55]
Modified array: [11, 0, 100, 11, 21]
Using the toInt() function to convert double to int in an array that contains values of both types. The
#foreach directive is used to loop over the array, the
#if directive and
type() function are used to check whether the type is not int, the
.remove() function is used to remove the original double value and the
.add() function is used to add the new int value.
#set($array=[10.6, 2, 99, 11.32, 20.55])
Original array: $array
#set($size=$array.size())
#foreach($i in [1..$size])
#set($index=$i-1)
#set($type=$array[$index].type())
#if($type != 'int')
#set($int=$array[$index].toInt())
$array.remove($index)
$array.add($index,$int)
#end
#end
Modified array: $array
Original array: [10.6, 2, 99, 11.32, 20.55]
Modified array: [11, 2, 99, 11, 21]
Using the toString() function to convert int values to string values.
#set($array=[100,200,300])
Integer addition:
#evaluate($array[0]+$array[2])
String addition:
#evaluate($array[0].toString()+$array[2].toString())
Integer addition:
400
String addition:
100300