IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
   ► KBTo/From GuidesReferenceDelphiData Structures  Print This     

Delphi Data Structures

Array

Languages Focus

A data structure in which individual values (called elements or items) may be located by reference to one or more integer index variables, the number of such indices being the number of dimensions in the array.

Arrays can start with an index value of 0 or 1, sometimes referred to as 0 based or 1 based.

Delphi:   x=Array[0..3] of string;

Delphi supports both static and dynamic arrays as well as single and multi dimensional arrays.

Syntax Example:
var
  MyArray: array[0..3] of string;
  i: Integer;
begin
  MyArray[0] := 'Mike';
  MyArray[1] := 'Lisa';
  MyArray[2] := 'Felicia';
  MyArray[3] := 'Nathan';
  
  for i := 0 to High(MyArray) do
    ShowMessage(MyArray[i]);
end;

0-Based, 1-Based: A Choice

Because you can specify both he upper and lower index values when you declare a static array, you can specify 0 or 1 as the base index value. For example, you can rewrite the above code as:

var
  MyArray: array[1..4] of string;
  i: Integer;
begin
  MyArray[1] := 'Mike';
  MyArray[2] := 'Lisa';
  MyArray[3] := 'Felicia';
  MyArray[4] := 'Nathan';
  
  for i := Low(MyArray) to High(MyArray) do
    ShowMessage(MyArray[i]);
end;

Note the use of Low() in place of the base index value in the for loop.

In fact, you could specify any range you wish,

MyArray: array[6..9] of string;

or even:

MyArray: array[-1..2] of string;

You can also use an ordinal type such as Word or Byte directlry. For exaple, because a Byte is equivalent to 0..255 the following:

MyArray: array[Byte] of string;

is equivalent to:

MyArray: array[0..255] of string;

Initialize Global Arrays

You can initialize the values of a global array when you declare it, but not a local array. For example, the following code snippet is in the interface section of a unit.

var
  Form2: TForm2;
  MyArray2: array[0..1] of string = ('Mike', 'Lisa');

 



Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Beginner

1 Beginner Level Question

Question #1: Multiple Choice

Which is the correct syntax for declaring an array and setting two values of the array?

Answer:
1. 
Dim MyArray As Variant
MyArray = Array("Mike", "Lisa")
2. 
var
  MyArray Array[2] String
endVar
MyArray[1] = "Mike"
MyArray[2] = "Lisa"
3. 
var
  MyArray: array[0..3] of string;
begin
  MyArray[0] := 'Mike';
  MyArray[1] := 'Lisa';
end;
4. 
var
  MyArray: array[0..3] of string;
begin
  MyArray(0) = 'Mike';
  MyArray(1) = 'Lisa';
end;
5. 
var
  MyArray array(0..3) : string;
begin
  MyArray[0] := 'Mike';
  MyArray[1] := 'Lisa';
end;

More Info





Sales Website: www.prestwood.com Or visit our legacy sales site: 
legacy.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]