您不必在声明中指定元素数目,但如果指定了元素数目,则该数目指示元素的初始数目。还可以通过在声明中列示一系列数组常量来指定元素的初始数目,只能对基本变量这样做,不能对记录这样做。
以下示例显示声明动态数组的语法:
// An array of 5 elements or less myDataItem01 CHAR(30)[] { maxSize=5 }; // An array of 6 elements or less, // with 4 elements initially myDataItem02 myDataItemPart[4] { maxSize=6 }; // An array that has no elements // but whose maximum size is the largest possible myRecord myRecordPart[]; // A 3-element array whose elements // are assigned the values 1, 3, and 5 position int[] = [1,3,5];
可使用文字整数来初始化元素数目,但变量和常量都无效。
// Valid, with maxsize giving the maximum // for the first dimension myInt01 INT[3][]; myInt02 INT[4][2][] {maxsize = 12}; myInt03 INT[7][3][1]; // In the next example, array constants indicate // that the outer array initially has 3 elements. // The first element of the outer array is // an array of two elements (with values 1 and 2). // The second element of the outer array is // an array of three elements (with values 3, 4,5). // The third element of the outer array is // an array of two elements (with values 6 and 7). myInt04 INT[][] = [[1,2],[3,4,5],[6,7]];
// NOT valid myInt04 INT[][3]; myInt05 INT[5][][2];
被指定为程序或函数参数的数组不能指定元素数目。
如果代码引用动态数组但未指定带方括号的下标,则引用的是整个数组。
内存不足状态被视为灾难性错误,并将导致程序结束。
series.reSize(100);
series INT[][]; // resizes the second element // of series, which is an array of arrays series[2].reSize(100);
在下面的描述中,用数组名替换 arrayName,并请注意,可以通过包名和/或库名来对名称进行限定:
appendArray 的元素必须与 arrayName 的元素具有相同的类型。
赋值指定了文字赋值规则。
content 是新内容(常量或变量,并具有适合于数组的类型),index 是指示新元素位置的整数文字或数字变量。
如果 index 比数组中的元素数目大 1,则此函数在数组末尾创建一个新元素并将数组大小增大 1。
index 是一个整数文字或数字变量,它指示要除去的元素的位置。
Record myFixedRecordPart 10 mySi CHAR(1)[3]; end
如果名为 myRecord 的固定记录基于该部件,则符号 myRecord.mySi 指的是包含三个元素的一维数组,其中每个元素都是一个字符。
本身不是数组的数组元素是与任何其它项类似的字段,可以通过各种方式引用该字段;例如,在赋值语句中引用它,或者在函数调用中作为自变量来引用它。
通过先指定数组名称并接着指定用方括号括起来的下标,可以引用一维数组(如 myRecord.mySi)的元素。下标是一个整数或解析为整数的字段;例如,可以使用 myStruct.mySi[2] 来引用示例数组中的第二个元素。下标的变化范围可以是从 1 到结构字段的 occurs 值,如果下标超出该范围,则会发生运行时错误。
如果在需要字段的上下文中使用结构字段数组的名称,但未指定用方括号括起来的下标,则 EGL 假定您正在引用数组的第一个元素,但仅当处于 VisualAge® Generator 兼容性方式时才如此。建议您显式地标识每个元素。如果未处于 VisualAge Generator 兼容性方式,则需要显式地标识每个元素。
// these refer to the first of three elements: myRecord.mySi[valueOne] // not recommended; and valid // only if VisualAge Generator // compatibility is in effect: myRecord.mySi // this refers to the second element: myRecord.mySi[valueTwo]
record myRecord01Part 10 name[3]; 20 firstOne CHAR(20); 20 midOne CHAR(20); 20 lastOne CHAR(20); end
如果名为 myRecord01 的记录基于上一个部件,则符号 myRecord01.name 指的是包含三个元素的一维数组,其中每个元素都带有 60 个字符,myRecord01 的长度是 180。
可以在不引用子结构的情况下引用 myRecord01.name 中的每个元素;例如,myRecord01.name[2] 引用第二个元素。也可以引用元素中的子结构。例如,如果满足唯一性规则,则可以按照下列任何方式来引用第二个元素的后 20 个字符:
myRecord01.name.lastOne[2] myRecord01.lastOne[2] lastOne[2]
仅当可生成部件属性 allowUnqualifiedItemReferences 设置为 yes 时,最后两种方法才有效。
有关不同类型的引用的详细信息,请参阅对变量和常量的引用。
如果 occurs 值大于 1 的结构项带有子结构,并且如果下级结构项也具有大于 1 的 occurs 值,则下级结构项声明带有附加维的数组。
record myRecord02Part 10 siTop[3]; 20 siNext CHAR(20)[2]; end
// row 1, column 2. // the next syntax is strongly recommended // because it works with dynamic arrays as well myRecord02.siTop[1].siNext[2] // the next syntax is supported for compatibility // with VisualAge Generator myRecord02.siTop.siNext[1,2]
要阐明引用了哪个内存区域,应了解多维数组中的数据是如何存储的。在当前示例中,myRecord02 包含 120 个字节。引用的区域被划分为具有三个元素的一维数组,每个元素 40 个字节:
siTop[1] siTop[2] siTop[3]
一维数组的每个元素都进一步细分为一个包含两个元素的数组,其每个元素均为 20 个字节,并且位于同一个内存区域中:
siNext[1,1] siNext[1,2] siNext[2,1] siNext[2,2] siNext[3,1] siNext[3,2]
// i, j, myTopOccurs, and myNextOccurs are data items; // myRecord02 is a record; and // sysLib.size() returns the occurs value of a structure item. i = 1; j = 1; myTopOccurs = sysLib.size(myRecord02.siTop); myNextOccurs = sysLib.size(myRecord02.siTop.siNext); while (i <= myTopOccurs) while (j <= myNextOccurs) myRecord02.siTop.siNext[i,j] = "abc"; j = j + 1; end i = i + 1; end
必须为多维数组的每个维指定值。例如,对于二维数组,引用 myRecord02.siTop.siNext[1] 是无效的。
record myRecord03Part 10 siTop[3]; 20 siNext[2]; 30 siLast CHAR(20)[5]; end
// each level is shown, and a subscript // is on each level, as is recommended. myRecord03.siTop[3].siNext[2].siLast[5] // each level shown, and subscripts are on lower levels myRecord03.siTop.siNext[3,2].siLast[5] myRecord03.siTop.siNext[3][2].siLast[5] // each level is shown, and subscripts are on the lowest level myRecord03.siTop.siNext.siLast[3,2,5] myRecord03.siTop.siNext.siLast[3,2][5] myRecord03.siTop.siNext.siLast[3][2,5] myRecord03.siTop.siNext.siLast[3][2][5] // the container and the last level is shown, with subscripts myRecord03.siLast[3,2,5] myRecord03.siLast[3,2][5] myRecord03.siLast[3][2,5] myRecord03.siLast[3][2][5] // only the last level is shown, with subscripts siLast[3,2,5] siLast[3,2][5] siLast[3][2,5] siLast[3][2][5]
如上一示例中所示,可通过添加用括号括起来的一组下标以任意方式引用多维数组的元素。在所有情况下,第一个下标表示第一个维,第二个下标表示第二个维,依此类推。每个下标的变化范围可以是从 1 到相关结构项的 occurs 值,如果某个下标解析为该范围外的数字,则会发生运行时错误。
myRecord03.siTop.siNext.siLast
myRecord03.siLast
siLast
myRecord03.siTop[3].siNext[2].siLast[5]
myRecord03.siTop.siNext[3,2].siLast[5]
myRecord03.siTop.siNext.siLast[3,2,5]
// NOT valid myRecord03.siTop[3,2,5].siNext.siLast
myRecord03.siTop.siNext.siLast[3,2,5] myRecord03.siTop.siNext.siLast[3,2][5] myRecord03.siTop.siNext.siLast[3][2,5] myRecord03.siTop.siNext.siLast[3][2][5]