In this section, we will explore two fundamental data structures in Delphi/Object Pascal: arrays and strings. Understanding these structures is crucial for handling collections of data and text manipulation efficiently.

Arrays

What is an Array?

An array is a collection of elements of the same type, stored in contiguous memory locations. Arrays allow you to store multiple values in a single variable, which can be accessed using an index.

Declaring Arrays

In Delphi, arrays can be declared in several ways. Here are some examples:

var
  IntArray: array[1..5] of Integer;
  CharArray: array[0..9] of Char;
  FloatArray: array[1..3] of Double;

Initializing Arrays

You can initialize arrays at the time of declaration or later in the code:

var
  IntArray: array[1..5] of Integer = (1, 2, 3, 4, 5);
  CharArray: array[0..9] of Char = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');

Accessing Array Elements

Array elements are accessed using their index. The index of the first element is usually 0 or 1, depending on how the array is declared.

begin
  IntArray[1] := 10; // Assigning value to the first element
  WriteLn(IntArray[1]); // Output: 10
end;

Iterating Through Arrays

You can use loops to iterate through arrays:

var
  i: Integer;
begin
  for i := 1 to 5 do
    WriteLn(IntArray[i]);
end;

Dynamic Arrays

Dynamic arrays are arrays whose size can be changed at runtime. They are declared without specifying the size:

var
  DynArray: array of Integer;
begin
  SetLength(DynArray, 10); // Setting the size of the array to 10
  DynArray[0] := 1;
  DynArray[1] := 2;
  // ...
end;

Strings

What is a String?

A string is a sequence of characters used to represent text. In Delphi, strings are managed types, meaning their memory is automatically managed by the compiler.

Declaring Strings

Strings are declared using the string type:

var
  MyString: string;

Initializing Strings

Strings can be initialized at the time of declaration or later in the code:

var
  MyString: string;
begin
  MyString := 'Hello, World!';
end;

Accessing String Characters

You can access individual characters in a string using an index:

begin
  MyString := 'Delphi';
  WriteLn(MyString[1]); // Output: D
end;

String Operations

Delphi provides various functions for string manipulation:

  • Concatenation: Combining two or more strings.
var
  Str1, Str2, ResultStr: string;
begin
  Str1 := 'Hello';
  Str2 := 'World';
  ResultStr := Str1 + ' ' + Str2; // ResultStr: 'Hello World'
end;
  • Length: Getting the length of a string.
begin
  WriteLn(Length(MyString)); // Output: 5 for 'Delphi'
end;
  • Copy: Extracting a substring.
begin
  WriteLn(Copy(MyString, 1, 3)); // Output: Del
end;
  • Pos: Finding the position of a substring.
begin
  WriteLn(Pos('phi', MyString)); // Output: 4
end;

Practical Exercises

Exercise 1: Array Manipulation

Task: Create an array of integers, initialize it with values, and find the sum of all elements.

var
  IntArray: array[1..5] of Integer = (1, 2, 3, 4, 5);
  Sum, i: Integer;
begin
  Sum := 0;
  for i := 1 to 5 do
    Sum := Sum + IntArray[i];
  WriteLn('Sum of array elements: ', Sum); // Output: 15
end;

Exercise 2: String Manipulation

Task: Create a string, find its length, and extract a substring.

var
  MyString: string;
  SubStr: string;
begin
  MyString := 'Object Pascal';
  WriteLn('Length of string: ', Length(MyString)); // Output: 13
  SubStr := Copy(MyString, 8, 6);
  WriteLn('Extracted substring: ', SubStr); // Output: Pascal
end;

Common Mistakes and Tips

  • Index Out of Bounds: Ensure that you do not access array elements outside their declared range.
  • String Indexing: Remember that string indexing starts at 1, not 0.
  • Dynamic Arrays: Always use SetLength to define the size of a dynamic array before accessing its elements.

Conclusion

In this section, we covered the basics of arrays and strings in Delphi/Object Pascal. Arrays allow you to store and manipulate collections of data, while strings enable efficient text handling. Understanding these concepts is essential for more advanced programming tasks, such as data processing and user input handling. In the next section, we will delve into records and sets, which provide more complex data structures for organizing and managing data.

Delphi/Object Pascal Programming Course

Module 1: Introduction to Delphi/Object Pascal

Module 2: Control Structures and Procedures

Module 3: Working with Data

Module 4: Object-Oriented Programming

Module 5: Advanced Delphi Features

Module 6: GUI Development with VCL and FMX

Module 7: Web and Mobile Development

Module 8: Best Practices and Design Patterns

Module 9: Final Project

© Copyright 2024. All rights reserved