可以通过下面含有关键字 Array 的类型名称来声明数组变量。
示例
//Declare x to be a Global variable of //Number Array type Global NumberVar Array x := [10 , 20, 30]; //cost is a Global variable of Currency Array type //It is automatically Global since the scope specifier //(one of Local, Global or Shared) is omitted. CurrencyVar Array cost := [$19.95, $79.50, $110.00, $44.79, $223.99]; //payDays is a Global variable of Date Array type Global DateVar Array payDays := [CDate(1999, 5, 15), CDate(1999, 5, 31)]; //y is a Shared variable of String Range Array type Shared StringVar Range Array y := ["A" To "C", "H" To "J"]; //days is a Local variable of String Array type Local StringVar Array days; days := ["Sun", "Mon", "Tue", "Wed", "Th", "Fri", "Sat"];
可以给数组的元素赋值,也可以使用元素的值进行其他计算。
示例
StringVar Array x := ["hello", "bye", "again"]; x [2] := "once"; //Now x is ["hello", "once", "again"] //The expression below would cause an error if not //commented out since the array has size 3 //x [4] := "zap"; //The formula returns the String "HELLO" UpperCase (x [1])
如果想在数组上添加额外的信息,可以使用 Redim 和 Redim Preserve 关键字调整数组的大小。Redim 首先清除数组原先的内容,然后调整数组大小,而 Redim Preserve 则保留原先的内容。
Local NumberVar Array x; Redim x [2]; //Now x is [0, 0] x [2] := 20; //Now x is [0, 20] Redim x [3]; //Now x is [0, 0, 0] x [3] := 30; //Now x is [0, 0, 30] Redim Preserve x [4]; //Now x is [0, 0, 30, 0] "finished" Local StringVar Array a; Redim a [2]; //Assign a value to the first element of the array a a[1] := "good"; a[2] := "bye"; //The & operator can be used to concatenate strings a[1] & a[2] //The formula returns the String "goodbye"
数组通常用于 For 循环。下例使用 For 循环创建并使用数组 [10, 20, 30, ..., 100]。
Local NumberVar Array b; Redim b[10]; Local NumberVar i; For i := 1 To 10 Do ( b[i] := 10 * i ); b [2] //The formula returns the Number 20