Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 03-10-2014
Avatar de BDWONG
BDWONG BDWONG is offline
Miembro
NULL
 
Registrado: nov 2013
Posts: 113
Poder: 11
BDWONG Va por buen camino
Cool Retonar arreglo dinamico en funcion

Bueno estaba mirando algo de poo en pascal y queria crear una funcion que me retornara
un arreglo intente poner la funcion y de retorno algo asi

function mifuncion:array of integer;
begin
end;

pero me daba error asi que cheque dos posible soluciones una declarar un nuevo tipo que fuera de array of integer
ejemplo

type
TIntegerArray:array of integer;

function mfuncion:TintegerArray;
begin
end;

y la otra solucion seria usar el Tarray<integer> como tipo de retorno la verdad me gusto mas la segunda opcion pero no se si estoy haciendo una buena implementacion ya que este Tarray<> tiene una serie funciones mas avanzadas

este codigo hice en delphi
Código Delphi [-]
unit Metodos;

interface

type
  TMetodo=class(TObject)
    private
    arreglo:Tarray;
    public
    constructor Create(tamano:integer);
    procedure LlenarArreglo;
    function  ObtenerArreglo():Tarray;
  end;


implementation

  constructor TMetodo.Create(tamano:integer);
  begin
     SetLength(self.arreglo,tamano);
  end;

  procedure TMetodo.LlenarArreglo;  {llenamos el arreglo}
  var
  i:integer;
  begin
    Randomize;
    for I := 0 to High(self.arreglo) do
    begin
        self.arreglo[i]:=Random(100);
    end;
  end;

  function TMetodo.ObtenerArreglo:Tarray;{RETORNAMOS el arreglo}
  begin
     if length(self.arreglo)>0 then
       result:=self.arreglo
     else
       result:=nil;
  end;

end.



entonces quiseira saber si es correcto como lo estoy haciendo
espero sus respuestas saludos...
Responder Con Cita
  #2  
Antiguo 04-10-2014
Avatar de gatosoft
[gatosoft] gatosoft is offline
Miembro Premium
 
Registrado: may 2003
Ubicación: Bogotá, Colombia
Posts: 833
Poder: 22
gatosoft Va camino a la fama
Bueno, en principio, tu codigo funciona... aunque de acuerdo a o que hablaste, creo que omitiste la definición: <Integer>

Código Delphi [-]
  TMetodo=class(TObject)
    private
    arreglo:Tarray;
    public
    constructor Create(tamano:integer);
    procedure LlenarArreglo;
    function  ObtenerArreglo():Tarray;
  end;

Por otro lado... quisiera saber cual es el objetivo de tu clase tMetodo.. y cual es la necesidad que tienes... pues tengo algunas dudas sobre la forma como implementas, pero eso depende de tu necesidad.

Podrias por ejemplo, no utilizar TArray<Integer>, sino TList<T>...

Código Delphi [-]

Uses system.Generics.Collections;

TMyArreglo = Class(TList)
end;



Es una lista dinnámica con la que te evitas el SetLength... y puedes utilizar metodos como Add, Insert, Delete, sort.... Pero claro está eso depende de tu necesidad...

Con Generics vienen predefindas unas estructuras interesantes para trabajar Pilas, Colas, Estructuras clave/valor (diccionarios), entre otras.

TObjectList
TStack
TQueue
TDictionary

en general, lo que quiero decir es que hablar de array dinámicos hoy dia no es muy común...

saludo
Responder Con Cita
  #3  
Antiguo 04-10-2014
Avatar de BDWONG
BDWONG BDWONG is offline
Miembro
NULL
 
Registrado: nov 2013
Posts: 113
Poder: 11
BDWONG Va por buen camino
Es cierto

la verdad tienes razon en lo Tarray<integer>

pero lo raro que yo en mi codigo si lo puse asi: Tarray<integer> no se si al momento de publicarlo se habra eliminado no lo se, ademas que que en un Tarray si no especificas de que tipo es te generar un error, inclusive si lo ocuparas con una variable de tipo variant

y el otro punto de cual era el objetivo, pues no existe alguno solo conocer en mas profunidad como funciona el lenguaje
ya que claro en esto tiempo seria mas optimo utilizar listas, pilas, colas etc.
pero solo seria probar si el retorno de la funcion seria lo mas ideal.
Responder Con Cita
  #4  
Antiguo 04-10-2014
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Poder: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola.

Solo una aclaración sobre la devolución de arreglos dinámicos. Como está escrita la funcion ObtenerArreglo en el mensaje de BDWONG, no se obtendría una copia del arreglo sino un apuntador al arreglo como se puede comprobar con este ejemplo:

Código Delphi [-]
program Project2; {$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TArray = array of Integer;

var
  vec: TArray;

// Devuelve apuntador
function RetArrayPtr: TArray;
begin
  Result:= vec;
end;

// Devuelve copia
function RetArray: TArray;
begin
  SetLength(Result, Length(vec));
  move(vec[0], Result[0], Length(vec)*Sizeof(Integer));
end;

var
  v1,v2: TArray;
  i: Integer;
begin
  SetLength(vec, 10);
  for i:= 0 to 9 do vec[i]:= i;

  v1:= RetArrayPtr;
  v2:= RetArray;

  Write('v1, apuntador:');
  for i:= 0 to 9 do write(v1[i]:3);
  Writeln;
  Write('v2, copia    :');
  for i:= 0 to 9 do write(v2[i]:3);
  Writeln;

  vec[0]:= 99;
  Writeln(#10#13+'Luego de modificar arreglo original'+#10#13);
  Write('v1, apuntador:');
  for i:= 0 to 9 do write(v1[i]:3);
  Writeln;
  Write('v2, copia    :');
  for i:= 0 to 9 do write(v2[i]:3);

  Finalize(vec);
  Finalize(v1);
  Finalize(v2);
  Readln;
end.

Saludos
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita
  #5  
Antiguo 05-10-2014
Avatar de Al González
[Al González] Al González is offline
In .pas since 1991
 
Registrado: may 2003
Posts: 5.604
Poder: 30
Al González Es un diamante en brutoAl González Es un diamante en brutoAl González Es un diamante en brutoAl González Es un diamante en bruto
Hola chicos.

Sólo agregar que las matrices (por muchos años les llamé arreglos) dinámicas, como también las cadenas de caracteres, utilizan contadores de referencias, por lo cual se copian automáticamente si y sólo si "algo" necesita una copia particular. Lo anterior ocurre, por ejemplo, cuando se agregan nuevos elementos a la matriz: sólo la variable usada en tal operación adquiere una copia, modificada, del contenido.

Eso hace de las matrices dinámicas una herramienta muy eficiente para el manejo colectivo de datos. Su uso está vigente en innumerables casos, y aunque existen clases más especializadas, no son nada obsoletas.

Saludos cordiales.

Al González.

P.D. Qué difícil es redactar desde un teléfono móvil.
Responder Con Cita
  #6  
Antiguo 05-10-2014
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Poder: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Cita:
Empezado por Al González Ver Mensaje
Eso hace de las matrices dinámicas una herramienta muy eficiente para el manejo colectivo de datos. Su uso está vigente en innumerables casos, y aunque existen clases más especializadas, no son nada obsoletas.
Totalmente de acuerdo , y realmente son una herramienta muy poderosa, también los uso a menudo.

Quizá fué mala elección usar el modo consola para representar el ejemplo dando así la impresión de que su uso es obsoleto. Pero es que me pareció mas claro de visualizar de ese modo, a ver si puedo enmendarlo un poco ...
Código Delphi [-]
{$WARNINGS OFF}
unit uDynArray;

interface

uses SysUtils, Classes;

type
  TElementType = Integer;
  TDynArray = array of TElementType;
  EDynRangeError = class(ErangeError);

  TClassDynArray = class(TObject)
  private
    FArray: TDynArray;
    FCount: Integer;
    FRandRange: Integer;
    function CheckInRange(Index: Integer): Boolean;
    function GetItem(index: Integer): TElementType;
    procedure SetItem(index: Integer; Value: TElementType);
    procedure QSortAsc(Left, Right: Integer);
    procedure QSortDes(Left, Right: Integer);
    function GetFArray: TDynArray;
  public
    constructor Create(NewLength: Integer=0);
    procedure RndFillArray;
    procedure Sort(const Ascending: Boolean=True);
    procedure Push(Value: TElementType);
    function Pop: TElementType;
    procedure Insert(Value: TElementType; Index: Integer);
    procedure InsertRange(Index: Integer; Values: array of TElementType);
    function IndexOf(Value: TElementType): Integer;
    procedure Delete(const Index: Integer);
    procedure DeleteRange(const a,b: Integer);
    procedure Swap(const a, b: Integer);
    procedure Increase(const Value: Integer);
    procedure Assign(DynArray: TDynArray);
    procedure FillZero;
    procedure Clear;
    //...
    destructor Destroy; override;
    property Count: Integer read FCount;
    property RandRange: Integer read FRandRange write FRandRange;
    property Items[index: Integer]: TElementType read GetItem write SetItem; default;
    property GetArray: TDynArray read GetFArray;
    //...
  end;

implementation

uses Windows;

// ------------------ public ------------------

constructor TClassDynArray.Create(NewLength: Integer);
begin
  Randomize;
  FRandRange:= 100;
  SetLength(FArray, NewLength);
  FCount:= NewLength;
end;

// Llena el arreglo con valores enteros de forma aleatoria
procedure TClassDynArray.RndFillArray;
var
  i: Integer;
begin
  for i:= 0 to FCount-1 do
    FArray[i]:= Random(FrandRange);
end;

// Agrega un elemento al final del arreglo
procedure TClassDynArray.Push(Value: TElementType);
begin
  SetLength(FArray, Length(FArray)+1);
  Inc(FCount);
  FArray[FCount-1]:= Value;
end;

// Extrae un elemento del final del arreglo
function TClassDynArray.Pop: TElementType;
begin
  Result:= FArray[FCount-1];
  Dec(FCount);
  SetLength(FArray, Length(FArray));
end;

// Inserta un elemento en la posición del índice
procedure TClassDynArray.Insert(Value: TElementType; Index: Integer);
var
  i: Integer;
begin
  if Index < 0 then
    raise EDynRangeError.Create('Índice fuera de rango');
  if Index > FCount then
    Push(Value)
  else
  begin
    SetLength(FArray, Length(FArray)+1);
    Inc(FCount);
    for i:= FCount-1 downto Index+1 do
      FArray[i]:= FArray[i-1];
    FArray[Index]:= Value;
  end;
end;

// Inserta un rango de valores en FArray
procedure TClassDynArray.InsertRange(Index: Integer; Values: array of TElementType);
var
  i: Integer;
begin
  for i:= Low(Values) to High(Values) do
    Insert(Values[i], i+1);
end;

// Devuelve el índice de la primera ocurrencia de Value
function TClassDynArray.IndexOf(Value: TElementType): Integer;
var
  i: Integer;
begin
  Result:= -1;
  for i:= 0 to FCount-1 do
    if FArray[i] = Value then
    begin
      Result:= i;
      Exit;
    end;
end;

// Elimina un elemento del arreglo
procedure TClassDynArray.Delete(const Index: Integer);
var
  i: Integer;
begin
  if FCount = 0 then
    raise EDynRangeError.Create('Arreglo vacío');
  if not CheckInRange(Index) then
    raise EDynRangeError.Create('Índice fuera de rango');
  for i:= Index to FCount-1 do FArray[i]:= FArray[i+1];
  Dec(FCount);
  SetLength(FArray, Length(FArray));
end;

// Elimina desde/hasta elementos del arreglo
procedure TClassDynArray.DeleteRange(const a, b: Integer);
var
  i: Integer;
begin
   if FCount = 0 then
    raise EDynRangeError.Create('Arreglo vacío');
  if not (CheckInRange(a)and(CheckInRange(b))) then
    raise EDynRangeError.Create('Índice fuera de rango');
  for i:= a to FCount-1 do
    FArray[i]:= FArray[b+i-1];
  Dec(FCount, Abs(a-b));
  SetLength(FArray, FCount)
end;

// Intercambia dos elementos del arreglo
procedure TClassDynArray.Swap(const a, b: Integer);
var
  aux: TElementType;
begin
  if not CheckInRange(a) or not CheckInRange(b) then
    EDynRangeError.Create('Indice fuera de rango');
  aux:= FArray[a];
  FArray[a]:= FArray[b];
  FArray[b]:= aux;
end;

// Llama a ordenamientos Asc o Desc acorde al valor del argumento
procedure TClassDynArray.Sort(const Ascending: Boolean=True);
begin
  if Ascending then
    QSortAsc(0, FCount-1)
  else
    QSortDes(0, FCount-1);
end;

// Aumenta el tamaño del arreglo
procedure TClassDynArray.Increase(const Value: Integer);
begin
  try
    SetLength(FArray, Length(FArray) + Value);
    Inc(FCount, Value);
  except
    raise EDynRangeError.Create('Memoria insuficiente');
  end;
end;

// Asigna un arreglo dinámico a FArray
procedure TClassDynArray.Assign(DynArray: TDynArray);
begin
  Clear;
  try
    FCount:= Length(DynArray);
    SetLength(FArray, FCount);
    move(DynArray[0], FArray[0], FCount*sizeof(TElementType));
  except
    raise EDynRangeError.Create('Memoria insuficiente');
  end;
end;

// Pone en cero los elementos del arreglo
procedure TClassDynArray.FillZero;
begin
  ZeroMemory(@FArray[0], Count*SizeOf(TElementType));
end;

// Elimina los elementos del arreglo
procedure TClassDynArray.Clear;
begin
  SetLength(FArray, 0);
end;

destructor TClassDynArray.Destroy;
begin
  Finalize(FArray);
  inherited;
end;

// ------------------ private ------------------

// Devuevelve el elemento indicado por el índice
function TClassDynArray.GetItem(index: Integer): TElementType;
begin
  if not CheckInRange(Index) then
    raise EDynRangeError.Create('Índice fuera de rango');
  Result:= FArray[index];
end;

// Asigna valor a un elemento del arreglo
procedure TClassDynArray.SetItem(index: Integer; Value: TElementType);
begin
  if not CheckInRange(Index) then
    raise EDynRangeError.Create('Índice fuera de rango');
  if FArray[index] <> Value then
    FArray[index]:= Value;
end;

// Devuelve False si el índice esta fuera del rango
function TClassDynArray.CheckInRange(Index: Integer): Boolean;
begin
  Result:= (Index >= 0) or (Index < FCount-1);
end;

// Ordena ascendente
procedure TClassDynArray.QSortAsc(Left, Right: Integer);
var
   i, j, mid: Integer;
begin
  i := Left;
  j := Right;
  mid := FArray[(Left+Right) div 2];
  repeat
    while FArray[i] < mid do Inc(i);
    while Farray[j] > mid do Dec(j);
    if i <= j then
    begin
      Swap(i, j);
      Inc(i);
      Dec(j);
    end;
  until i > j;
  if j > Left then QSortAsc(Left, j);
  if i < Right then QSortAsc(i, Right);
end;

// Ordena descendente
procedure TClassDynArray.QSortDes(Left, Right: Integer);
var
  i, j, mid : Integer;
begin
  i:= Left;
  j:= Right;
  mid := FArray[(Left+Right) div 2];
  repeat
    while FArray[i] > mid do Inc(i);
    while FArray[j] < mid do Dec(j);
    if i <= j then
    begin
      Swap(i, j);
      Inc(i);
      Dec(j);
    end;
  until i >= j;
  if j > Left then QSortDes(Left, j);
  if i < Right then QSortDes(i, Right);
end;

// Devuelve una copia del arreglo
function TClassDynArray.GetFArray: TDynArray;
begin
  try
    SetLength(Result, FCount);
    move(FArray[0], Result[0], FCount*sizeof(TElementType));
  except
    raise EDynRangeError.Create('Memoria insuficiente');
  end;
end;
end.
Y se le puede ir agregando las funcionalidades que se nos vayan ocurriendo.

Cita:
Empezado por Al González Ver Mensaje
P.D. Qué difícil es redactar desde un teléfono móvil.
De echo yo no me animo

Saludos
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita
  #7  
Antiguo 05-10-2014
Avatar de Al González
[Al González] Al González is offline
In .pas since 1991
 
Registrado: may 2003
Posts: 5.604
Poder: 30
Al González Es un diamante en brutoAl González Es un diamante en brutoAl González Es un diamante en brutoAl González Es un diamante en bruto
Cita:
Empezado por ecfisa Ver Mensaje
Quizá fué mala elección usar el modo consola para representar el ejemplo dando así la impresión de que su uso es obsoleto.
Despreocúpate ecfisa, tu código siempre me ha parecido enriquecedor. Fue gatosoft quien al parecer apuntaba lo de la obsolescencia (quizá me equivoco).
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
funcion que me regresa un arreglo julyus OOP 14 27-08-2010 23:49:01
Funcion Array Dinamico??? franss Varios 4 28-08-2008 21:33:23
Arreglo de Registros, con arreglos de Registros(Dinamico) PiornoCKA&G Varios 4 05-01-2007 08:53:30
arreglo dinamico prubtest C++ Builder 1 31-07-2004 23:54:45
Como devuelvo un Arreglo en Función Prophoenix Varios 1 30-05-2003 19:40:58


La franja horaria es GMT +2. Ahora son las 07:27:54.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi