The Delphi Language, Part 1 - Arrays
(Page 7 of 10 )
The Delphi language enables you to create arrays of any type of variable. For example, a variable declared as an array of eight integers reads like this:
var A:
Array[0..7] of Integer; This statement is equivalent to the following C# declaration:
int A[8]; It's also equivalent to this Visual Basic .NET statement:
Dim A(8) as Integer Delphi arrays have a special property that differentiates them from other languages: They don't have to be based at a certain number. You can therefore declare a three-element array that starts at 28, as in the following example:
var
A: Array[28..30] of Integer; Because Delphi arrays aren't guaranteed to begin at 0 or 1, you must use some care when iterating over array elements in a for loop. The compiler provides built-in functions called High() and Low(), which return the lower and upper bounds of an array variable or type, respectively. Your code will be less error prone and easier to maintain if you use these functions to control your for loop, as shown here:
var
A: array[28..30] of Integer;
i: Integer;
begin
for i := Low(A) to High(A) do // don't hard-code for loop!
A[i] := i;
end;
To specify multiple dimensions, use a comma-delimited list of bounds:
var
// Two-dimensional array of Integer:
A: array[1..2, 1..2] of Integer;
To access a multidimensional array, use commas to separate each dimension within one set of brackets:
I := A[1, 2];
Dynamic Arrays
Dynamic arrays are dynamically allocated arrays in which the dimensions aren't known at compile time. To declare a dynamic array, just declare an array without including the dimensions, like this:
var
// dynamic array of string:
SA: array of string; Before you can use a dynamic array, you must use the SetLength() procedure to allocate memory for the array:
begin
// allocate room for 33 elements:
SetLength(SA, 33); Once memory has been allocated, you can access the elements of the dynamic array just like a normal array:
SA[0] := 'Pooh likes hunny';
OtherString := SA[0]; Note - Dynamic arrays are always zero-based.
Dynamic arrays are lifetime managed by the .NET runtime, so there's no need (and, in fact, no way) to free them when you're through using them because they will be garbage collected at some point after leaving scope. However, there might come a time when you want to request that the .NET runtime remove the dynamic array from memory before it leaves scope (if it uses a lot of memory, for example). To do this, you need only assign the dynamic array to nil:
SA := nil; // requests release of SA Note that assigning to nil does not release SA; it only releases a reference to it, as there could be more than one variable referencing the array indicated by SA. When the last reference to SA is released, the .NET garbage collector will release the memory occupied by the array during the next garbage collection.
Dynamic arrays are manipulated using reference semantics rather than value semantics like a normal array. Here's a quick test: What is the value of A1[0] at the end of the following code fragment?
var
A1, A2: array of Integer;
begin
SetLength(A1, 4);
A2 := A1;
A1[0] := 1;
A2[0] := 26;
The correct answer is 26 because the assignment A2 := A1 doesn't create a new array but instead provides A2 with a reference to the same array as A1. Therefore, any modifications to A2 will also affect A1. If you want instead to make a complete copy of A1 in A2, use the Copy() standard procedure:
A2 := Copy(A1); After this line of code is executed, A2 and A1 will be two separate arrays initially containing the same data. Changes to one will not affect the other. You can optionally specify the starting element and number of elements to be copied as parameters to Copy(), as shown here:
// copy 2 elements, starting at element one:
A2 := Copy(A1, 1, 2); Dynamic arrays can also be multidimensional. To specify multiple dimensions, add an additional array of to the declaration for each dimension:
var
// two-dimensional dynamic array of Integer:
IA: array of array of Integer; To allocate memory for a multidimensional dynamic array, pass the sizes of the other dimensions as additional parameters to SetLength():
begin
// IA will be a 5 x 5 array of Integer
SetLength(IA, 5, 5); You access multidimensional dynamic arrays the same way you do normal multidimensional arrays; each element is separated by a comma with a single set of brackets:
IA[0,3] := 28; C-style sytanx for multidimensional array access is also supported:
IA[0][3] := 28;
This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Records and Sets >>
More .NET Articles
More By Xavier Pacheco