Welcome to the new DelphiDabbler Code Library Documentation.

This is a new site that's currently running on alpha code. There are going to be bugs. If you discover any, please report them on the site's issues page (GitHub account required). Thanks.

Warning: Many URLs are going to change. Refer to the README file to discover which library project's documentation has been completed.

Delete<T> class method

Project: Array Utilities Unit

Unit: DelphiDabbler.Lib.ArrayUtils

Record: TArrayUtils

Applies to: ~>0.1

class procedure Delete<T>(var A: TArray<T>; const AIndex: Integer);
  overload; static;

class procedure Delete<T>(var A: TArray<T>;
  const AIndices: array of Integer);
  overload; static;

Description

Deletes the element or elements at one or more indices in an array.

Parameters:

Examples

Example #1

This example demonstrates the overload of Delete<T> that deletes a single element an array.

procedure Delete_Eg1;
var
  A, Expected: TArray<Integer>;
begin
  A := TArray<Integer>.Create(1, 2, 3, 4, 5);

  // delete middle item
  TArrayUtils.Delete<Integer>(A, 2);
  Expected := TArray<Integer>.Create(1, 2, 4, 5);
  Assert(TArrayUtils.Equal<Integer>(Expected, A));

  // delete first item
  TArrayUtils.Delete<Integer>(A, 0);
  Expected := TArray<Integer>.Create(2, 4, 5);
  Assert(TArrayUtils.Equal<Integer>(Expected, A));

  // delete last item
  TArrayUtils.Delete<Integer>(A, 2);
  Expected := TArray<Integer>.Create(2, 4);
  Assert(TArrayUtils.Equal<Integer>(Expected, A));

  // delete invalid index < 0
  TArrayUtils.Delete<Integer>(A, -1);
  Expected := TArray<Integer>.Create(2, 4);
  Assert(TArrayUtils.Equal<Integer>(Expected, A));

  // delete invalid index = Length(A)
  TArrayUtils.Delete<Integer>(A, Length(A));
  Expected := TArray<Integer>.Create(2, 4);
  Assert(TArrayUtils.Equal<Integer>(Expected, A));
end;

Example #2

This second example shows the use of the Delete<T> overload that deletes a elements at multiple indices.

procedure Delete_Eg2;
var
  A, Expected: TArray<Integer>;
begin
  // Delete indices 0, 2, 4 and 9. We also throw in some duplicate and out of
  // range indices to show that they are ignored.
  A := TArray<Integer>.Create(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  // the following function is identical to calling
  // TArrayUtils.Delete<Integer>(A, [0, 2, 4, 9]);
  TArrayUtils.Delete<Integer>(A, [0, 26, 2, -1, 9, 4, 9, 9, 26]);
  Expected := TArray<Integer>.Create(1, 3, 5, 6, 7, 8);
  Assert(TArrayUtils.Equal<Integer>(Expected, A));
end;

See Also