TBetterArray<T>
is just a workaround to provide instance methods for arrays.
The generic class TArray<T>
was introduced in Deplhi XE2 to give a better approach of manipulating dynamic arrays.
Although it was a huge step for delphi, TArray<T>
is painful to deal with, any single interation requires lots of code.
The usage is basically the same of TArray<T>
, however there are some examples described bellow.
For the complete list of available methods, please check the source and the examples at the test unit.
var
People: TBetterArray<string>;
begin
People := ['Fernando', 'John', 'Paul'];
People.Count; // 3
People.Join('-'); // 'Fernando-John-Paul'
People.First; // 'Fernando'
People.Contains('Trump'); // False
People.Reverse; // ['Paul', 'John', 'Fernando']
People.Add('Bill'); // ['Paul', 'John', 'Fernando', 'Bill']
end;
TCoffee = class
Name: string;
Origin: string;
Strength: Integer;
constructor Create(Name, Origin: string; Strength: Integer);
end;
...
var
Coffees: TBetterArray<TCoffee>;
begin
Coffees := [
TCoffee.Create('Arabic', 'Egypt', 5),
TCoffee.Create('Turkish', 'Turkey', 8),
TCoffee.Create('Capuccino', 'Italy', 2)
];
end;
var
StrengthComparison: TComparison<TCoffee>;
begin
StrengthComparison :=
function(const Left, Right: TCoffee): Integer
begin
Result := Left.Strength - Right.Strength;
end;
Coffees.Sort(StrengthComparison);
Coffees.First.Name; // 'Capuccino'
Coffees.Last.Name; // 'Turkish'
Coffees[1]; // TCoffee instance
Coffees[5]; // Error
Coffees.Get(1); // TCoffee instance
Coffees.Get(5); // nil