Home > Articles > Programming > General Programming/Other Languages

This chapter is from the book

This chapter is from the book

Arrays

Pascal is a very clean and easy-to-read language. If you can write code at all, you should be able to understand the basics of the Pascal syntax. The Pascal array syntax is no exception to this rule. Artfully constructed and robustly engineered, Pascal arrays are a powerful feature of the language.

Here is how to declare an array of Integers in Pascal:

procedure TForm1.Button1Click(Sender: TObject);
var
  MyArray: array [0..10] of Integer;
  MyNum: Integer;
begin
  MyArray[0] := 1;
  MyNum := MyArray[0];
end;

In this code, you find an array of 11 Integers. The first member of the array is at offset 0, and the last is at offset 10. The syntax for capturing this construction is simplicity itself. It is hard to imagine how the concept of an array could be expressed more elegantly or more clearly.

Pascal arrays are very flexible. Note that you can declare not only the top of the range of values in an array, but also the bottom:

procedure TForm1.Button1Click(Sender: TObject);
var
  MyArray: array [12..24] of Integer;
  MyNum: Integer;
begin
  MyArray[12] := 1;
  MyNum := MyArray[12];
end;

In this example, the first element of the array is at offset 12, and the last is at 24. You can discover at runtime the range of values in an array:

procedure TForm1.Button1Click(Sender: TObject);
var
  MyArray: array [12..24] of Integer;
  HighValue, LowValue: Integer;
begin
  HighValue := High(MyArray);
  LowValue := Low(MyArray);
  Label1.Caption := `High Value: ` + IntToStr(HighValue);
  Label2.Caption := `Low Value: ` + IntToStr(LowValue);
end;

The High function returns the upper limit of an array, which, in this case, is 24. Low returns the low range, which, in this case, is 12.

Array Constants

When I pick up a new language, I always seem to struggle to find an example of how to declare array constants. This next sample shows two array constants, one with Integers and the other with Strings:

procedure TForm1.Button1Click(Sender: TObject);
type
  TMyStringArray = array[0..2] of String;
const
  MyIntArray: array[0..3] of Integer = (1, 2, 3, 4);
  MyStringArray: TMyStringArray = (`One', `Two', `Three');
begin
  ListBox1.Items.Add(IntToStr(MyIntArray[0])); // prints 1
  ListBox1.Items.Add(MyStringArray[0]); // prints `one'
end;

In this example, I declare the type of the Integer array in the const statement and the type of the String array in a type section. I do this simply to show that, depending on your needs or preferences, you can use either syntax.

Dynamic Arrays and Arrays of Objects

Unlike C/C++, Pascal has no trouble handling arrays of objects. The following syntax discovers all the buttons on a form, places them in an array, and gives you access to their properties and methods:

procedure TForm1.Button3Click(Sender: TObject);
var
  ButtonArray: array of TButton;
  i, j, Num: Integer;
begin
  Num := 0;
  for i := 0 to ComponentCount - 1 do
    if Components[i] is TButton then
      Inc(Num);

  SetLength(ButtonArray, Num);
  j := 0;
  for i := 0 to ComponentCount - 1 do
    if Components[i] is TButton then begin
      ButtonArray[j] := TButton(Components[i]);
      Inc(j);
    end;

  for i := 0 to High(ButtonArray) do
    ListBox1.Items.Add(ButtonArray[i].Caption);
end;

The Button3Click method shows the Pascal language doing all sorts of glorious thing to delight and amuse us. The code discovers all the TButton objects on a form, declares an array just large enough to hold these buttons, and then places the buttons in the array. Finally, the captions of each button are placed in a list box, as shown in Figure 3.1.

ButtonArray is declared to be an array of TButton, with no declared bottom or top range. Instead, the range of the array will be discovered at runtime.

Figure 3.1Figure 3.1 A list box is put to use holding the captions of each button placed on this form.

To find out how many elements we need in the array, the code makes use of information maintained by the main form of this application. In particular, all forms maintain a list of the components that have been dropped on them.

The program uses the Form1.ComponentCount property to determine how many components are on the form:

for i := 0 to ComponentCount - 1 do

Then each element of the array of components maintained by the main form is iterated over and is checked to see if it is a button:

if Components[i] is TButton then

Perhaps some clarity could be gained if these methods were written thus:

procedure TForm1.Button3Click(Sender: TObject);
var
  ButtonArray: array of TButton;
  i, j, Num: Integer;
begin
  Num := 0;
  for i := 0 to Self.ComponentCount - 1 do
    if Self.Components[i] is TButton then
      Inc(Num);

In this rewrite of the first lines of the Button3Click method, I make explicit the fact that ComponentCount and the Components array both belong to Form1.

C/C++, JAVA NOTE

Object Pascal uses the term Self where Java or C++ would use this. Depending on your frame of mind, this personification of objects could be appealing or distracting. Despite the different flavors associated with the two terms, their meaning is ultimately identical.

When you know the number of buttons that were dropped on the form, you can set the size of the array of buttons:

 SetLength(ButtonArray, Num);

The SetLength method does exactly what you would expect: It allocates memory in the array for the number of buttons that you have dropped on the form.

NOTE

You will perhaps recall that SetLength can also be used to set the length as an AnsiString. Because a string is really just an array of Char, this is not quite the coincidence that it might appear at first. Other functions that you can use on both types include Copy and Length.

The next chunk of code simply iterates over the components again, adding the buttons into the array as each is discovered:

  for i := 0 to ComponentCount - 1 do
    if Components[i] is TButton then begin
      ButtonArray[j] := TButton(Components[i]);
      Inc(J);
    end;

Finally, the code displays the names of the buttons in a list box:

  for i := 0 to High(ButtonArray) do
    ListBox1.Items.Add(ButtonArray[i].Caption);

Note that the syntax for accessing the members of the array of buttons is utterly clean and intuitive:

ButtonArray[i].Caption

It turns out that the SetLength procedure can be used not only to allocate memory for a String, but also to reallocate it. The following rewrite of the Button3Click method shows how to put this to use:

procedure TForm1.Button3Click(Sender: TObject);
var
  ButtonArray: array of TButton;
  i, Num: Integer;
begin
  Num := 0;
  for i := 0 to Self.ComponentCount - 1 do
    if Self.Components[i] is TButton then begin
      Inc(Num);
      SetLength(ButtonArray, Num);
      ButtonArray[Num - 1] := TButton(Components[i]);
    end;

  for i := 0 to High(ButtonArray) do
    ListBox1.Items.Add(ButtonArray[i].Caption);
end;

Here the method is shortened and cleaned up a bit by continually reallocating the memory for the ButtonArray with the SetLength method.

NOTE

You might expect that repeatedly reallocating memory has some overhead associated with it, so it is possible that this second solution is not necessarily any faster than the first technique. People who are interested in such matters can explore the matter further on their own. Unless you are using these methods in a large, frequently called loop, don't worry about using one method or the other—they both execute in essentially zero time.

If you want to shorten the length of a dynamic array, use the Copy function:

procedure TForm1.Button5Click(Sender: TObject);
const
  BigLen = 24;
  SmallLen = 12;
var
  MyArray: array of Double;
  i: Integer;
begin
  SetLength(MyArray, BigLen);
  for i := 0 to High(MyArray) do
    MyArray[i] := Sqr(i);

  MyArray := Copy(MyArray, 0, SmallLen);

  for i := 0 to High(MyArray) do
    ListBox1.Items.Add(Format(`Value of array = %f', [MyArray[i]]));
end;

This code first sets the length of MyArray to 24. Next it assigns values to each member of the array. The array is then shortened to length 12. Finally, the program displays the elements of the array to show that the shortening did not destroy the values that it held. Note that you can use High, Low and Length on dynamic arrays, just as you can on normal arrays and on strings.

Pascal arrays can contain multiple dimensions. Here is an example of how multi-dimensional arrays works:

procedure TForm1.Button6Click(Sender: TObject);
const
  XSize = 5;
  YSize = 5;
var
  MyTwoDim: array[0..XSize, 0..YSize] of Integer;
  i, j: Integer;
begin
  for i := 0 to YSize do
    for j := 0 to XSize do
      MyTwoDim[j, i] := j * i;

  for i := 0 to YSize do
    for j := 0 to XSize do
      StringGrid1.Cells[j, i] := IntToStr(MyTwoDim[j, i]);
end;

The code declares a two-dimensional array of Integer. Both dimensions contain five elements. The code then fills out the array with the first five elements of the multiplication table. A TStringGrid allows the program to display the content of the array, as shown in Figure 3.2. The key to the TStringGrid object is the Cells property, which represents the array of cells in the grid. The syntax for using the Cells property is the same as for using a two-dimensional array of String.

Figure 3.2Figure 3.2 The TStringGrid object from the Additional page in the Component Palette can help you display your data in a clean and logical fashion.

You may declare an array in the normal fashion and then pass it to a method as a parameter. In such cases, you need not specify the size of the parameter in the function that you pass to it:

procedure TForm1.ShowArray(MyArray: array of String);
var
  i: Integer;
begin
  for i := 0 to High(MyArray) do
    ListBox1.Items.Add(MyArray[i]);
end;

procedure TForm1.Button7Click(Sender: TObject);
var
  MyArray: array [0..2] of String;
begin
  MyArray[0] := `You need not declare the size';
  MyArray[1] := `of an array you pass to a method.';
  MyArray[2] := `Instead, just declare an open array';
  ShowArray(MyArray);
end;

Here you can see that I do not explicitly state the size of the array to be passed to the ShowArray method. Instead, I declare it as an open array of String and then use the High method to determine its actual dimensions.

Debug Your Arrays: Turn on Range Checking

When developing applications that use arrays, it is best to select Project, Options, Compiler and turn on Range Checking. Range Checking is off, by default. This is a technique used by the compiler to check that you are not trying to write past the end of an array. For instance, if you allocate an array of 10 bytes in size and try to write to the nonexistent 11th byte of the array, the Range Checking mechanism would catch the error and raise an exception. Without range checking, sometimes writing past the end of an array will raise an exception; at other times, it will corrupt the memory of a program in some subtle and seemingly undetectable manner. This can lead to debugging sessions that last for hours or even days. If you turn on Range Checking, however, the error will appear immediately, and it will be clearly flagged. After debugging your code that uses arrays, you should turn off Range Checking because it slows down your code.

In this section on arrays, I discussed the basic facts about arrays in a few short paragraphs and then covered the more complex subject of dynamic arrays and open arrays. This look at arrays ended with a brief meditation on the importance of range checking.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020