FreePascal Information Logo Friend of FreePascal Compiler Title
Articles with Feedback, FPC News Library, PDF Collection, Mail Lists, Books, Newsgroups, IRC Open online discussion areas Research and Tutorials Tools, Compilers and Utilities Blurbs about us, advertising, etc.
Welcome to the FoFPC Pascal Language ARRAY

array


     The Array keyword provides single and multi dimensional arrays (indexable sequences) of data.

     Static arrays

     These are defined with fixed, unchangeable sizes. They may be single or multidimensional - the latter being an array of arrays (of arrays etc). The size and range of such a multidimensional array is always given for the highest, leftmost array - the parent array.

     The size of each dimension is determined in two ways, which may be freely mixed in a multidimensional array: (1) Index type Where Index is an integer type, normally Byte or Word. The range of this type defines the dimension range. For example, a Byte gives a 0..255 range. (2) Ordinal..Ordinal Alternatively, the range of each dimension may be given by direct ordinal values, such as 22..44.

     Dynamic arrays

     Dynamic arrays have no preallocated storage. When defined, only a pointer is created. Such arrays must have their length set before they can be used. For example: SetLength(dynArray, 5); sets the dynArray single dimension array size to 5 elements. This allocates storage.

     All dynamic arrays starts at index = 0.

     Individual subarrays of a multidimensional dynamic array may have different sized dimensions - they are, of course, separate arrays. After one such SetLength operation, elements of the set array may be referenced, even though the rest of the array is undefined.

Example

    Download Source IconDownload This Source for Free Pascal
var
  // Define static arrays
  wordArray  : Array[Word] of Integer;     // Static, size=High(Word)
  multiArray : Array[Byte, 1..5] of char;  // Static array, 2 dimensions
  rangeArray : Array[5..20] of string;     // Static array, size = 16
  // Define dynamic arrays
  byteArray  : Array of Byte;                // Single dimension array
  DynMultiArray : Array of Array of string;  // Multi-dimension array
  // Worker variables
  i,j : Integer;

begin
  // Show the sizes and ranges of these arrays
  Writeln('wordArray length = ',Length(wordArray));
  Writeln('wordArray lowest element = ',Low(wordArray));
  Writeln('wordArray highest element = ',High(wordArray));
  Writeln('');
  Writeln('multiArray length = ',Length(multiArray));
  Writeln('multiArray lowest element = ',Low(multiArray));
  Writeln('multiArray highest element = ',High(multiArray));
  Writeln('');
  // The full range of a static array are available before assignment,
  // but the values will be unpredictable
  Writeln('wordArray Element 7 = ',wordArray[7]);
  Writeln('wordArray Element 20 = ',wordArray[20]);
  // Use indexing to furnish an array
  for i := 5 to 20 do
    Str(i * 5, rangeArray[i]);
  // Now use indexing to display 2 of the elements
  Writeln('rangeArray element 7 = '+rangeArray[7]);
  Writeln('rangeArray element 20 = '+rangeArray[20]);
  Writeln('');

  // Set the length of the single dimension array
  SetLength(byteArray, 5);

  // Show the size and range of this array
  Writeln('byteArray length = ',Length(byteArray));
  Writeln('byteArray lowest element = ',Low(byteArray));
  Writeln('byteArray highest element = ',High(byteArray));
  Writeln('');

  // Furnish this array - remember that dynamic arrays start at 0
  for i := 0 to 4 do
    byteArray[i] := i * 5;

  // Show selected elements from the array
  Writeln('byteArray element 2 = ',byteArray[2]);
  Writeln('byteArray element 4 = ',byteArray[4]);
  Writeln('');

  // Set the length of the 1st dimension of the multi-dim array
  SetLength(DynMultiArray, 3);

  // Set the length of the 3 sub-arrays to different sizes
  SetLength(DynMultiArray[0], 1);
  SetLength(DynMultiArray[1], 2);
  SetLength(DynMultiArray[2], 3);

  // Set and show all elements of this array
  for i := 0 to High(DynMultiArray) do
    for j := 0 to High(DynMultiArray[i]) do begin
      Str(i*j, DynMultiArray[i,j]);
      Writeln('DynMultiArray[',i,',',j,'] = ',DynMultiArray[i,j]);
    end;
end.

Output

wordArray length = 65536
wordArray lowest element = 0
wordArray highest element = 65535

multiArray length = 256
multiArray lowest element = 0
multiArray highest element = 255

wordArray Element 7 = 0
wordArray Element 20 = 0
rangeArray element 7 = 35
rangeArray element 20 = 100

byteArray length = 5
byteArray lowest element = 0
byteArray highest element = 4

byteArray element 2 = 10
byteArray element 4 = 20

DynMultiArray[0,0] = 0
DynMultiArray[1,0] = 0
DynMultiArray[1,1] = 1
DynMultiArray[2,0] = 0
DynMultiArray[2,1] = 2
DynMultiArray[2,2] = 4

See Also

Copy, High, Length, Low, Move, SetLength, Slice.
 Links and Products we find useful



ButtonGenerator.com
Valid XHTML 1.0 Transitional Internet Map
Programmer's Heaven
grat-i-fi-ca-tion - noun
the state of being gratified; great satisfaction.


I am very happy to see that FoFPC is alive, we need more developers working on this great language.
Matias Vara
toro.sourceforge.net
Locations of visitors to this page world map hits counter
Copyright 2009 by 3F, LLC. All rights reserved. Worldwide.
Your request was processed by server #3 in 0.295519 secs.

sponsor
Click to visit our paid sponsor