Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've demonstrated how to delete items from a dynamic array before:</p> <ul> <li><a href="http://www.cs.wisc.edu/~rkennedy/array-delete" rel="noreferrer">Delphi Q&amp;A: How do I delete an element from an array?</a></li> </ul> <p>In that article, I start with the following code:</p> <pre><code>type TXArray = array of X; procedure DeleteX(var A: TXArray; const Index: Cardinal); var ALength: Cardinal; i: Cardinal; begin ALength := Length(A); Assert(ALength &gt; 0); Assert(Index &lt; ALength); for i := Index + 1 to ALength - 1 do A[i - 1] := A[i]; SetLength(A, ALength - 1); end; </code></pre> <p>You <em>cannot go wrong</em> with that code. Use whatever value for <code>X</code> you want; in your case, replace it with <code>string</code>. If you want to get fancier and use <code>Move</code>, then there's way to do that, too.</p> <pre><code>procedure DeleteX(var A: TXArray; const Index: Cardinal); var ALength: Cardinal; TailElements: Cardinal; begin ALength := Length(A); Assert(ALength &gt; 0); Assert(Index &lt; ALength); Finalize(A[Index]); TailElements := ALength - Index; if TailElements &gt; 0 then Move(A[Index + 1], A[Index], SizeOf(X) * TailElements); Initialize(A[ALength - 1]); SetLength(A, ALength - 1); end; </code></pre> <p>Since <code>X</code> is <code>string</code>, the <code>Finalize</code> call is equivalent to assigning the empty string to that array element. I use <code>Finalize</code> in this code, though, because it will work for <em>all</em> array-element types, even types that include records, interfaces, strings, and other arrays.</p> <p>For inserting, you just shift things the opposite direction:</p> <pre><code>procedure InsertX(var A: TXArray; const Index: Cardinal; const Value: X); var ALength: Cardinal; TailElements: Cardinal; begin ALength := Length(A); Assert(Index &lt;= ALength); SetLength(A, ALength + 1); Finalize(A[ALength]); TailElements := ALength - Index; if TailElements &gt; 0 then begin Move(A[Index], A[Index + 1], SizeOf(X) * TailElements); Initialize(A[Index]); A[Index] := Value; end; </code></pre> <p>Use <code>Finalize</code> when you're about to do something that's outside the bounds of the language, such as using the non-type-safe <code>Move</code> procedure to overwrite a variable of a compiler-managed type. Use <code>Initialize</code> when you're re-entering the defined part of the language. (The language defines what happens when an array grows or shrinks with <code>SetLength</code>, but it doesn't define how to copy or delete strings without using a string-assignment statement.)</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