此处所列的文章均是我自己从国外的网站摘抄并翻译的,由于英文水平有限,里面肯定有不少错漏.翻译这些东西没有其他的什么用途,只是提高自己的英语阅读能力和编程技术水平而已     

数组2

Delphi Basics
Array
Keyword
A data type holding indexable collections of data
1   type Name = array[Index type|Ordinal..Ordinal {,...}] of Base type; // Static array
2   type Name = array of {array of ...} Base type; // Dynamic array
3   Name : array of {array of ...} const; // Open variant array
  Name : Array type; // Open dynamic array
Example code : Declaring and using static arrays
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

  i : Integer;

begin
  // Show the sizes and ranges of these arrays
  ShowMessage('wordArray length = '+IntToStr(Length(wordArray)));
  ShowMessage('wordArray lowest element = '+IntToStr(Low(wordArray)));
  ShowMessage('wordArray highest element = '+IntToStr(High(wordArray)));
  ShowMessage('multiArray length = '+IntToStr(Length(multiArray)));
  ShowMessage('multiArray lowest element = '+IntToStr(Low(multiArray)));
  ShowMessage('multiArray highest element = '+IntToStr(High(multiArray)));
  ShowMessage('rangeArray length = '+IntToStr(Length(rangeArray)));
  ShowMessage('rangeArray lowest element = '+IntToStr(Low(rangeArray)));
  ShowMessage('rangeArray highest element = '+IntToStr(High(rangeArray)));
  ShowMessage('');

  // The full range of a static array are available before assignment,
  // but the values will be unpredictable
  ShowMessage('wordArray Element 7 = '+IntToStr(wordArray[7]));
  ShowMessage('wordArray Element 20 = '+IntToStr(wordArray[20]));

  // Use indexing to furnish an array
  for i := 5 to 20 do
    rangeArray[i] := IntToStr(i * 5);

  // Now use indexing to display 2 of the elements
  ShowMessage('rangeArray element 7 = '+rangeArray[7]);
  ShowMessage('rangeArray element 20 = '+rangeArray[20]);
end;

Show full unit code
   wordArray length = 65536
   wordArray lowest element = 0
   wordArray highest element = 65535
   multiArray length = 256
   multiArray lowest element = 0
   multiArray highest element = 255
   rangeArray length = 16
   rangeArray lowest element = 5
   rangeArray highest element = 20
  
   wordArray element 7 = 0
   wordArray element 20 = 0
   rangeArray element 7 = 35
   rangeArray element 20 = 100
  
 
 
Example code : Declaring and using dynamic arrays
var
  // Define dynamic arrays
  byteArray  : Array of Byte;           // Single dimension array
  multiArray : Array of Array of string;  // Multi-dimension array

  i,j : Integer;

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

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

  // 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
  ShowMessage('byteArray element 2 = '+IntToStr(byteArray[2]));
  ShowMessage('byteArray element 4 = '+IntToStr(byteArray[4]));

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

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

  // Set and show all elements of this array
  for i := 0 to High(multiArray) do
    for j := 0 to High(multiArray[i]) do
    begin
      multiArray[i,j] := IntToStr(i+j);
      ShowMessage('multiArray['+intToStr(i)+','+intToStr(j)+'] = '+
                  multiArray[i,j]);
    end;
end;

Show full unit code
   byteArray length = 5
   byteArray lowest element = 0
   byteArray highest element = 4
   byteArray element 2 = 10
   byteArray element 4 = 20
   multiArray[0,0] = 0
   multiArray[1,0] = 1
   multiArray[1,1] = 2
   multiArray[2,0] = 2
   multiArray[2,1] = 3
   multiArray[2,2] = 4
  
 
 
Example code : Using open arrays as parameters
var
  // Define a dynamic array
  charArray : TCharArray;
  openArray : Array [0..2] of char;

  i : Integer;

begin
  // Pass the undefined array as a dynamic array to a subroutine
  FurnishDynamicArray(charArray);

  // Furnish an array for the next routine
  openArray[0] := 'N';
  openArray[1] := 'o';
  openArray[2] := 'w';

  // Pass this predefined array as an open array to a subroutine
  ShowOpenTypeArray(openArray);

  // Show all elements of the passed array
  for i := 0 to High(charArray) do
      ShowMessage('charArray['+intToStr(i)+'] = '+charArray[i]);

  // Pass a number of characters as an open constant array to a subroutine
  ShowOpenConstArray(['H','e','l','l','o']);
end;

// 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
  // Set the length of the single dimension array
  SetLength(typeArray, 5);

  // Furnish this array - remember that dynamic arrays start at 0
  for i := 0 to 4 do
    typeArray[i] := Chr(Ord('A') + i);
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
  // Show all elements of the passed array
  // IMPORTANT - we assume here that the constant types are all char
  //             See the TVarRec type for more on Variant types.
  for i := 0 to High(constArray) do
    ShowMessage('constArray['+intToStr(i)+'] = '+constArray[i].VChar);
end;

Show full unit code
   typeArray[0] = N
   typeArray[1] = o
   typeArray[2] = w
   charArray[0] = A
   charArray[1] = B
   charArray[2] = C
   charArray[3] = D
   charArray[4] = E
   constArray[0] = H
   constArray[1] = e
   constArray[2] = l
   constArray[3] = l
   constArray[4] = o
posted @ 2010-11-09 21:11  AppleAndPear  阅读(219)  评论(0编辑  收藏  举报