Liste

array of - classical dynamic array structure which can hold dynamic items of same type. Items are accessible by direct addressing SomeArray[Index]. SetLength and Length functions are used to handle size. TList - objective way where class handle all list operations. Implementation in LCL keep compatibility with VCL where TList holds list of Pointers. If list of different item type is desired complete new class have to be copied and rewritten or typecasting have to be used in every place where list item is referenced. Typecasting is not effective e.g. Pointer(Byte) and not type safe Pointer(Int64). TList index is of type Integer. If Int64 or SmallInt index is needed than class have to be copied and rewritten. TCollection - is more generic and type safe but heavier solution for storing list of items. In this approach programmer have to create item class which inherits from TCollectionItem and set this newly created class type to TCollection constructor parameter. native generics - not complete implementation yet. Lack support of generic type reference, inheritance, constraints. templates - use code inclusion to workaround lack of native support. Can use inheritance and generic type reference. Constraints are given by used operators and other functions in generic class.

  • TStringList è un discendente di TStrings.
  • TStringList sa come ordinare stessa in ordine alfabetico.
  • TStringList ha una proprietà di Objects, molto utile, che la rende l'equivalente di una
  • TStringList rendere il codice compatibile con tutte le versioni precedenti di Delphi.
  • TStringList può essere utilizzato come una proprietà pubblicato. (A bug impedisce classi generiche di essere pubblicati, per ora.)

TList è una lista di valori Pointer, lavorare con TList comporta usare pesantemente i puntatori

TStringList ha la base dei metodi di tipo TList e altri metodi personalizzati per lavorare con le stringhe, ad esempio. SaveToFile () e. LoadFromFile(), ordinamento

Array

dynamic array sono linked list. sono l'equivalente di std::vector in C++, ArrayList in Java e C#. esitono anche Generic List<> in Delphi10 e vari altri linguaggi.

program testarray3;
Type
  TA = array of Integer;
var
  A,B : TA;
  I : Integer;
begin
  Setlength(A,10);// alloca spazio
 
  For I:=Low(A) to High(A) do begin
      A[I]:=I;  // scrive valori
  end;
 
  // slice
  B:=Copy(A,3,6);
 
  For I:=0 to 5 do begin
    Writeln(B[I]);
  end;
end.

usa le funzioni built in per iterare in sicurezza gli array: Length() will return the number of elements in the array. Low() on a dynamic array will always return 0 High() will return the value Length-1, i.e., the value of the highest allowed array index.

si possono ottenere delle slice degli array con copy

es. di array con strutture

const max= 512;
type     MyRecord = record
                S1: String;
                i1 : Integer;
           end;
type MyRecordPtr =^MyRecord;
var     RecordCount:  Integer;
          MyRecordArray : array[1...max] of MyRecordPtr;
 
procedure CreateNewRecord;
begin
       inc(recordCount)
       New(MyRecordArray[recordCount]);
end;

usare array come parametri delle procedure:

// Procedure that updates a dynamic array size
// IMPORTANT - note that the array type must not be defined here -
//             we must use an array type to avoid the array being treated
//             as an open array.
procedure TForm1.FurnishDynamicArray(var typeArray : TCharArray);
var
  i : Integer;
begin
  SetLength(typeArray, 5); // Set the length of the array
 
  // Furnish this array - remember that dynamic arrays always start at 0
  for i := 0 to 4 do begin
    typeArray[i] := Chr(Ord('A') + i);
  end;
end;
 
// Procedure that takes an open array
procedure TForm1.ShowOpenTypeArray(typeArray : Array of char);
var
  i : Integer;
begin
  // Show all elements of the passed array
  for i := 0 to High(typeArray) do
    ShowMessage('typeArray['+intToStr(i)+'] = '+typeArray[i]);
end;
 
// Procedure that takes an open constant array
procedure TForm1.ShowOpenConstArray(const constArray : Array of const);
var
  i : Integer;
begin
  // See the TVarRec type for more on Variant types.
  for i := 0 to High(constArray) do
    ShowMessage('constArray['+intToStr(i)+'] = '+constArray[i].VChar);
end;

ritornare array dalle funzioni

la cosa non è semplice come dovrebbe: Arrays declared in type statements are dynamic arrays; arrays declared in procedure/function headers are open arrays. You can return a dynamic array, but not an open array.

in pratica: definisci un tipo "type ObjArr: array of TMyObj;" e ritorna quel tipo. la cosa è necessaria perchè gli array sono linked list implementate in assembler dal compilatore, che ha bisogno di riferisi a un tipo specifico per funzionare semplicemente.

program arrayTester;
{$APPTYPE CONSOLE}
uses
  SysUtils;
 
type
  tPerson = record
    name: string;
    age:  integer;
    end;
  tPeople = array of tPerson; // nota: questo è il trucco
 
var people: tPeople;
 
//procedure getPeople( var result: tPeople );  // Method 1
function getPeople: tPeople;  // Method 2
  var
    s: string;
    i: integer;
  begin
  i := 0;
  repeat
    setLength( result, i+1 );
    write( 'Please enter a name> ' );
    readln( result[ i ].name );
 
    write( 'Please enter ', result[ i ].name, '''s age> ' );
    readln( result[ i ].age );
    write( 'Are there more (yes/no)? ' );
    readln( s );
    inc( i )
  until upCase( s[ 1 ] ) = 'N'
  end;
 
begin
 
// Method 1
//getPeople( people );
 
// Method 2
people := getPeople;
 
writeln( 'You entered ', length( people ), ' people.' )
end.

TObjectBucket

controllare l'implementazione, sembra una linked list

Generic

Generic class is defined using keyword generic before class name and use in class declaration. @see fgl unit for more examples

type
  generic TList<T> = class
    Items: array of T;
    procedure Add(Value: T);
  end;
 
procedure TList.Add(Value: T);
begin
  SetLength(Items, Length(Items) + 1);
  Items[Length(Items) - 1] := Value;
end;
 
// Generic class can be simply specialized for particular type by use specialize keyword.
Type
  TIntegerList = specialize TList<Integer>;
  TPointerList = specialize TList<Pointer>;
  TStringList = specialize TList<string>;

cdecl

The cdecl modifier can be used to declare a function that uses a C type calling convention. This must be used if you wish to acces functions in an object file generated by a C compiler. It allows you to use the function in your code, and at linking time, you must link the object file containing the C implementation of the function or procedure. As an example:

program CmodDemo;
{$LINKLIB c}
Const P : PChar = 'This is fun !';
Function strlen (P : PChar) : Longint; cdecl; external;
begin
  WriteLn ('Length of (',p,') : ',strlen(p))
end.

When compiling this, and linking to the C-library, you will be able to call the strlen function throughout your program. The external directive tells the compiler that the function resides in an external object filebrary (see ). Remark: The parameters in our declaration of the C function should match exactly the ones in the declaration in C. Since C is case sensitive, this means also that the name of the function must be exactly the same. the Free Pascal compiler will use the name exactly as it is typed in the declaration.