myInts int[] = [1,2,3,4,5];
In
this case, the array is a series of five integer variables. In EGL,
arrays
begin numbering with the number one, so this array has elements numbered
one
through five.myInts[1] = 5+5;
myInts[2] = 16;
myInts[3] = myInts[1] + myInts[2];
myStrings string[2];
myStrings {"Hello", "Goodbye"};
Note that this syntax uses braces ({) and no equals sign (=). This is not an assignment statement but a method of assigning values to the array as though the elements were its properties. This method offers better performance at run time than the other method, which entails an array literal to the array.
Also, this method works only when the array has sufficient elements to accept the new values in braces; EGL does not automatically add more elements. For this reason, you must specify a starting length for the array or otherwise add elements to it before you can assign values to elements with a set-values block.
myBigInts bigint[];
myBigInts = [10,40];
This method is a little slower than
the set-value
method because the generated code must create two arrays: one for
the variable
and one for the literal.myStringsInit string[] {"Hello", "Goodbye"};
myBigIntsInit bigint[] = [10, 40];
myDecimals decimal(10,2)[3] {55.43, 22.12, 4.34, CurrencySymbol = "$"};
If
you are using the array literal method of specifying starting values,
you
can set properties with the set-values block as usual:myBools boolean[3]{MaxSize = 5} = [true, false, true];
fiveInts int[5];
SysLib.writeStderr(fiveInts[1]); //Writes "0"
It's good coding practice to specify a starting length
for the
array when you create it so that EGL can initialize it. You can always
add
or remove elements later with array functions such as appendElement and removeElement.nullArray int[];
nullArray[2] = 5; //NullValueException!
nullArray.appendElement(5); //NullValueException!
nullArray {1,2,3}; //NullValueException!
Instead, you must
begin
by initializing the array with an array literal:nullArray2 int[];
nullArray2 = [1,2,3];
nullArray2.appendElement(4);
emptyArray int[]{};
emptyArray.appendElement(5);
In the previous example, the
set-value
block is empty. You cannot use a set-values block to assign more elements
to
an array than currently exist in the array. This array has zero elements,
so you must use a set-values block with zero values.smallIntArray int[2];
smallIntArray = [1,2,3,4,5];
smallStrArray string[2];
smallStrArray {"ab", "cd", "ef", "gh"};
//IndexOutOfBoundsException! Array has only 2 elements!
For more details on arrays, see "Arrays" in the EGL Language Reference.