Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As of Delphi 4, Delphi supports <strong>dynamic arrays</strong>. You can modify their sizes at run time and they will retain the data you stored in other elements at the old size. They can hold elements of any homogeneous type, including records and other arrays. You can declare a dynamic array the same as you declare normal, "static" arrays, but simply omit the array bounds:</p> <pre><code>var ArthurArray: array of TForm; </code></pre> <p>Although static arrays allow you to specify both the lower and upper bound, the low index of a dynamic array is always zero. The high index is given by the <code>High</code> function, which always returns one less than the length of the array. For any dynamic array <code>x</code>, <code>High(x) = Length(x)-1</code>.</p> <p>A global variable can be accessed by any code, including local procedures.</p> <p>A global variable of dynamic-array type will be initialized to be an <em>empty</em> array. Its length will be zero and <code>High</code> called on that array will be -1. <code>Low</code> on that array will still return zero.</p> <p>At any time, you may resize a dynamic array. Use the <code>SetLength</code> function, just as you can do with strings:</p> <pre><code>var NumElements: Integer; begin NumElements := GetNumberOfArthurForms(); SetLength(ArthurArray, NumElements); end; </code></pre> <p>If you have a multidimensional array, you can set their lengths in a loop:</p> <pre><code>var matrix: array of array of Double; i: Integer; begin SetLength(matrix, height); for i := 0 to height - 1 do SetLength(matrix[i], width); end; </code></pre> <p>There's a shortcut for that to set the lengths of all the inner arrays at once:</p> <pre><code>begin SetLength(matrix, height, width); end; </code></pre> <p>Like I mentioned, dynamic arrays keep their old values when you resize them:</p> <pre><code>var data: array of string; begin SetLength(data, 2); data[1] := 'foo'; SetLength(data, 20); Assert(data[1] = 'foo'); end; </code></pre> <p>But if you <em>shorten</em> the array, any elements that resided beyond the new last element are gone forever:</p> <pre><code>begin SetLength(data, 20); data[15] := 'foo'; SetLength(data, 2); // data[15] does not exist anymore. SetLength(data, 16); writeln(data[15); // Should print an *empty* line. end; </code></pre> <p>My demonstrations above used strings. Strings are special in Delphi; they're managed by the compiler through reference counts. Because of that, new dynamic-array elements of type string are initialized to be empty. But if I had used integers instead, there would be no guarantee of the values of new elements. They might be zero, but they might be anything else, too, just like the initial values of standalone local variables.</p> <p>The Delphi 7 help files are very good, I'm told. Please read more about dynamic arrays there. You can find demonstrations of their use throughout the VCL and RTL source code provided in your Delphi installation, as well as in nearly any Delphi code example produced in the last 10 years.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload