Каноничный пример одномерного массива
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
var Arr: Variant; I: Integer; begin { Create a variant array of 10 elements, starting at 0 and ending at 9. The array contains elements of type integer } Arr := VarArrayCreate([0, 9], varInteger); { Increase the length of the variant array } VarArrayRedim(Arr, 49); MessageDlg('Variant array has ' + IntToStr(VarArrayDimCount(Arr)) + ' dimensions', mtInformation, [mbOK], 0); { Traverse the array from lower to higher bounds } for I := VarArrayLowBound(Arr, 1) to VarArrayHighBound(Arr, 1) do begin { Put the element I at index I} VarArrayPut(Arr, 'Привет', [I]); end; { Now read the elements of the array } for I := VarArrayLowBound(Arr, 1) to VarArrayHighBound(Arr, 1) do begin { Put the element I at index I} if VarArrayGet(Arr, [I]) <> I then MessageDlg('Error! Invalid element found at current position!', mtError, [mbOK], 0); end; { Clear the variant array } VarClear(Arr); end; |
Далее пример двумерного массива
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
procedure TMain.VarArrayExample2D; var arr2D: Variant; cols, rows: integer; i, j: integer; lowCols, lowRows: Integer; highCols, highRows: Integer; begin cols := 2; rows := 2; arr2D := VarArrayCreate([1, cols, 1, rows], varInteger); lowCols := VarArrayLowBound(arr2D, 1); highCols := VarArrayHighBound(arr2D, 1); lowRows := VarArrayLowBound(arr2D, 2); highRows := VarArrayHighBound(arr2D, 2); Memo1.Lines.Clear(); for i := lowCols to highCols do for j := lowRows to highRows do begin arr2D[i, j] := i.ToString() + ' ' + j.ToString(); Memo1.Lines.Add((arr2D[i, j])); end; end; |
Довольно хорошая документация по вариантным массивам от Emba