Ir al contenido


Foto

[RESUELTO] generar arrays


  • Por favor identifícate para responder
3 respuestas en este tema

#1 FELIX

FELIX

    Advanced Member

  • Miembros
  • PipPipPip
  • 123 mensajes

Escrito 27 julio 2011 - 05:13

saludos

alguien me podria decir como generar un array que no tenga un tamaño especifico??

  • 0

#2 Wilson

Wilson

    Advanced Member

  • Moderadores
  • PipPipPip
  • 2.137 mensajes

Escrito 27 julio 2011 - 06:59

Declarar un array dinámico es igual a declarar cualquier otro tipo de dato, lo que pasa es que antes de poder usarlo debes asignarle quieras o nó el tamaño. El siguiente ejemplo declara un tipo de array dinámico de string, asigna un campo privado de nombre FMiArray del tipo anteriormente declarado, con el primer boton configuramos la dimensión del array en función de la cantidad de líneas del memo1 y pasamos el contenido de las líneas del memo1 al FMiArray y con el segundo botón pasamos el contenido del FMiArray al memo2.


Prueba agregando diferente número de líneas al memo1 y pulsa primero el boton1 y luego el botón 2 para que veas los resultados.




delphi
  1. unit Unit1;
  2.  
  3.  
  4. interface
  5.  
  6.  
  7. uses
  8.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  9.   Dialogs, StdCtrls;
  10.  
  11.  
  12. Type
  13.   TMiarrayDinamico = Array of string;
  14.  
  15.  
  16. type
  17.   TForm1 = class(TForm)
  18.     Memo1: TMemo;
  19.     Memo2: TMemo;
  20.     Button1: TButton;
  21.     Button2: TButton;
  22.     procedure Button1Click(Sender: TObject);
  23.     procedure Button2Click(Sender: TObject);
  24.   private
  25.     FMiArray: TMiarrayDinamico;
  26.     { Private declarations }
  27.   public
  28.     { Public declarations }
  29.   end;
  30.  
  31.  
  32. var
  33.   Form1: TForm1;
  34.  
  35.  
  36. implementation
  37.  
  38.  
  39. {$R *.dfm}
  40.  
  41.  
  42. procedure TForm1.Button1Click(Sender: TObject);
  43. var
  44.   K: Integer;
  45. begin
  46.   SetLength(FMiArray, Memo1.Lines.Count);
  47.   for K := 0 to Memo1.Lines.Count - 1 do
  48.     FMiArray[K] := Memo1.Lines[K];
  49. end;
  50.  
  51.  
  52. procedure TForm1.Button2Click(Sender: TObject);
  53. var
  54.   K: Integer;
  55. begin
  56.   Memo2.Clear;
  57.   for K := Low(FMiArray) to High(FMiArray) do
  58.     Memo2.Lines.Add(FMiArray[K]);
  59. end;
  60.  
  61.  
  62. end.


  • 0

#3 FELIX

FELIX

    Advanced Member

  • Miembros
  • PipPipPip
  • 123 mensajes

Escrito 28 julio 2011 - 07:15

FUNCIONA PERFECTAMENTE GRACIAS POR TU AYUDA  (y) :smiley:
  • 0

#4 Rolphy Reyes

Rolphy Reyes

    Advanced Member

  • Moderadores
  • PipPipPip
  • 2.092 mensajes
  • LocationRepública Dominicana

Escrito 28 julio 2011 - 07:30

Saludos.

La ayuda de Delphi 7 es fabulosa:

Static array types are denoted by constructions of the form

array[indexType1, ..., indexTypen] of baseType

where each indexType is an ordinal type whose range does not exceed 2GB. Since the indexTypes index the array, the number of elements an array can hold is limited by the product of the sizes of the indexTypes. In practice, indexTypes are usually integer subranges.

In the simplest case of a one-dimensional array, there is only a single indexType. For example,



delphi
  1. var MyArray: array[1..100] of Char;



declares a variable called MyArray that holds an array of 100 character values. Given this declaration, MyArray[3] denotes the third character in MyArray. If you create a static array but don't assign values to all its elements, the unused elements are still allocated and contain random data; they are like uninitialized variables.

A multidimensional array is an array of arrays. For example,



delphi
  1. type TMatrix = array[1..10] of array[1..50] of Real;



is equivalent to



delphi
  1. type TMatrix = array[1..10, 1..50] of Real;



Whichever way TMatrix is declared, it represents an array of 500 real values. A variable MyMatrix of type TMatrix can be indexed like this: MyMatrix[2,45]; or like this: MyMatrix[2][45]. Similarly,



delphi
  1. packed array[Boolean,1..10,TShoeSize] of Integer;



is equivalent to



delphi
  1. packed array[Boolean] of packed array[1..10] of packed array[TShoeSize] of Integer;



The standard functions Low and High operate on array type identifiers and variables. They return the low and high bounds of the array's first index type. The standard function Length returns the number of elements in the array's first dimension.

A one-dimensional, packed, static array of Char values is called a packed string. Packed-string types are compatible with string types and with other packed-string types that have the same number of elements. See Type compatibility and identity.

An array type of the form array[0..x] of Char is called a zero-based character array. Zero-based character arrays are used to store null-terminated strings and are compatible with PChar values. See Working with null-terminated strings.


Dynamic arrays do not have a fixed size or length. Instead, memory for a dynamic array is reallocated when you assign a value to the array or pass it to the SetLength procedure. Dynamic-array types are denoted by constructions of the form

array of baseType

For example,



delphi
  1. var MyFlexibleArray: array of Real;



declares a one-dimensional dynamic array of reals. The declaration does not allocate memory for MyFlexibleArray. To create the array in memory, call SetLength. For example, given the previous declaration,



delphi
  1. SetLength(MyFlexibleArray, 20);



allocates an array of 20 reals, indexed 0 to 19. Dynamic arrays are always integer-indexed, always starting from 0.

Dynamic-array variables are implicitly pointers and are managed by the same reference-counting technique used for long strings. To deallocate a dynamic array, assign nil to a variable that references the array or pass the variable to Finalize; either of these methods disposes of the array, provided there are no other references to it. Dynamic arrays are automatically released when their reference-count drops to zero. Dynamic arrays of length 0 have the value nil. Do not apply the dereference operator (^) to a dynamic-array variable or pass it to the New or Dispose procedure.

If X and Y are variables of the same dynamic-array type, X := Y points X to the same array as Y. (There is no need to allocate memory for X before performing this operation.) Unlike strings and static arrays, COPY-ON-WRITE is not employed for dynamic arrays, so they are not automatically copied before they are written to. For example, after this code executes,



delphi
  1. var
  2.   A, B: array of Integer;
  3. begin
  4.   SetLength(A, 1);
  5.   A[0] := 1;
  6.   B := A;
  7.   B[0] := 2;
  8. end;


the value of A[0] is 2. (If A and B were static arrays, A[0] would still be 1.)

Assigning to a dynamic-array index (for example, MyFlexibleArray[2] := 7) does not reallocate the array. Out-of-range indexes are not reported at compile time.

In contrast, to make an independent copy of a dynamic array, you must use the global Copy function:



delphi
  1. var
  2.   A, B: array of Integer;
  3. begin
  4.   SetLength(A, 1);
  5.   A[0] := 1;
  6.   B := Copy(A);
  7.   B[0] := 2; { B[0] <> A[0] }
  8. end;


When dynamic-array variables are compared, their references are compared, not their array values. Thus, after execution of the code



delphi
  1. var
  2.   A, B: array of Integer;
  3. begin
  4.   SetLength(A, 1);
  5.   SetLength(B, 1);
  6.   A[0] := 2;
  7.   B[0] := 2;
  8. end;


A = B returns False but A[0] = B[0] returns True.

To truncate a dynamic array, pass it to SetLength, or pass it to Copy and assign the result back to the array variable. (The SetLength procedure is usually faster.) For example, if A is a dynamic array, A := SetLength(A, 0, 20) truncates all but the first 20 elements of A.

Once a dynamic array has been allocated, you can pass it to the standard functions Length, High, and Low. Length returns the number of elements in the array, High returns the array's highest index (that is, Length - 1), and Low returns 0. In the case of a zero-length array, High returns -1 (with the anomalous consequence that High < Low).

Note

In some function and procedure declarations, array parameters are represented as array of baseType, without any index types specified. For example,



delphi
  1. function CheckStrings(A: array of string): Boolean;



This indicates that the function operates on all arrays of the specified base type, regardless of their size, how they are indexed, or whether they are allocated statically or dynamically. See Open array parameters.

Multidimensional dynamic arrays
To declare multidimensional dynamic arrays, use iterated array of ... constructions. For example,



delphi
  1. type TMessageGrid = array of array of string;
  2. var Msgs: TMessageGrid;



declares a two-dimensional array of strings. To instantiate this array, call SetLength with two integer arguments. For example, if I and J are integer-valued variables,



delphi
  1. SetLength(Msgs,I,J);



allocates an I-by-J array, and Msgs[0,0] denotes an element of that array.

You can create multidimensional dynamic arrays that are not rectangular. The first step is to call SetLength, passing it parameters for the first n dimensions of the array. For example,



delphi
  1. var Ints: array of array of Integer;
  2. SetLength(Ints,10);


allocates ten rows for Ints but no columns. Later, you can allocate the columns one at a time (giving them different lengths); for example



delphi
  1. SetLength(Ints[2], 5);



makes the third column of Ints five integers long. At this point (even if the other columns haven't been allocated) you can assign values to the third column--for example, Ints[2,4] := 6.

The following example uses dynamic arrays (and the IntToStr function declared in the SysUtils unit) to create a triangular matrix of strings.



delphi
  1. var
  2.   A : array of array of string;
  3.   I, J : Integer;
  4. begin
  5.   SetLength(A, 10);
  6.   for I := Low(A) to High(A) do
  7.   begin
  8.     SetLength(A[I], I);
  9.     for J := Low(A[I]) to High(A[I]) do
  10.       A[I,J] := IntToStr(I) + ',' + IntToStr(J) + ' ';
  11.   end;
  12. end;



  • 0




IP.Board spam blocked by CleanTalk.