BDWONG,
Cita:
Empezado por BDWONG
...Retornar arreglo dinámico en función...conocer en mas profundidad como funciona el lenguaje Delphi...
|
Revisa este código:
Código Delphi
[-]
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Generics.Defaults, Generics.Collections;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
ListBox2: TListBox;
ListBox3: TListBox;
ListBox4: TListBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
private
public
end;
TIntegerComparer = TComparer;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
type
TArrayInteger = Array of Integer;
function FillArray(const InputArray : TArrayInteger) : TArrayInteger;
var
i : Integer;
begin
Randomize;
SetLength(Result,Length(InputArray));
for i:= Low(InputArray) to High(InputArray) do
Result[i] := InputArray[i] + Random(1000);
end;
var
A1, A2, A3, A4 : TArrayInteger;
i,p : Integer;
MsgUser : String;
SearchElement : Integer;
Comparer: IComparer;
begin
ListBox1.Clear;
ListBox2.Clear;
ListBox3.Clear;
ListBox4.Clear;
A1 := [2,1,5,3,4];
A2 := [19,21,78,15,14];
A3 := A1;
Insert(A2,A3,Length(A3));
A3 := A3 + [900,800,700,600];
Delete(A3,Length(A3)-4,2);
TArray.Sort(A3);
for i := Low(A3) to High(A3) do
ListBox1.Items.Add(IntToStr(A3[i]));
Comparer := TDelegatedComparer.Create(
function(const Left, Right: Integer) : Integer
begin
Result := Right - Left; end);
TArray.Sort(A3, Comparer);
for i := Low(A3) to High(A3) do
ListBox2.Items.Add(IntToStr(A3[i]));
SetLength(A3,Length(A3) + 2);
for i := Length(A3)-2 to High(A3) do
A3[i] := 7777 + i;
TArray.Sort(A3);
for i := Low(A3) to High(A3) do
ListBox3.Items.Add(IntToStr(A3[i]));
A4 := FillArray(A3);
TArray.Sort(A4, Comparer);
for i := Low(A4) to High(A4) do
ListBox4.Items.Add(IntToStr(A4[i]));
SearchElement := 21;
if TArray.BinarySearch(A3,SearchElement,p) then
begin
MsgUser := Format('El Elemento %d se encuentra en la Posición %d del Array A3 Modo-1',
[SearchElement,p]);
MessageDlg(MsgUser,mtInformation,[mbOK],0);
end;
Finalize(A1);
Finalize(A2);
Finalize(A3);
Finalize(A4);
end;
end.
El código anterior en Delphi XE7 sobre Windows 8.1 Professional x32,
ejemplifica el uso de arreglos dinámicos en Delphi XE7, según se muestra en la siguiente imagen:
Espero sea útil
Nelson.