Stratmoss
Stratmoss

Reputation: 19

Is it possible, in Delphi , to have an array inside another array?

Is it possible to store an array inside another array so that more data could be held.

For example an array contains 5 variables of strings called: Charlie, Tom, Harry, Jane and Josh. Were each of these then contains an array storing the names of their five friends.

Is this possible in Delphi?

The use of this: I am creating a program that takes in preference votes for five candidates, each candidate will need to have its own array were the first variable in the array shows how many first preferences they have and so on. I need to input this data into an array containing the names of the candidates, as already explained.

Upvotes: 0

Views: 1076

Answers (2)

Teun Pronk
Teun Pronk

Reputation: 1397

In addition to the answers @SilverWarrior provided there is another option. Although I do recommend the second approach of an array of record.

There are several ways to declare a multi-dimensional array.
Example 1:

type
  TMyArray = array of string; //or array[0..N] of string if it's fixed size
  TMyArrList = array of TMyArray;//or array[0..N] of TMyArray

Example 2:

var
  MyArray: array of array of string;//Dynamic array will require SetLength for both main and subarrays
  MyOtherArray: array[0..5] of array[0..5] of string;

Example 3: (fixed length only and if arrays are of the same type)

var
  MyArray[0..5, 0..5] of string;

Example 3 is what I usually do when I need a multidimensional array.

Another possibility for your example is the use of a TDictionary which you can find in Generics.Collections.
It could look like this:

type
  MyFriends = Array[0..4] of String;
//In class or function/procedure
var
  MyDict: TDictionary<String, MyFriends>;

Hope this helps you on the way a bit.

Upvotes: 0

SilverWarior
SilverWarior

Reputation: 8386

Is it possible to store one array in another array using Delphi? Yes it is. The simpliest way to implement this is like so:

//Standard one dimensional array of Strings
AMyArray: Array[0..5] of String;

//Array containing multiple Standard one dimensional arrays
//Or in other word two dimensional array
MYArrayColection: Array[0..4] of AMyArray;

Note in order to achieve what you wan't your one dimensional array must contain 6 elements. First element stores the name of your person. Next five store the names of his/hers friends

But this is a bad design. Why?
If we look at your example Charlie, Tom and Hary can all have Jane as a fried. This means that you will be storing her name miltiple times and doing so consuming more memory.
Now with small number of pepole and smal number of firends this doesen't pose a problem but when you have large number of pepole with bigger number of friends (which you might want later) this can become a real problem since you could be wasting large amount of memor becouse of this.

First instead of storing persons information in string store it in record. This record should have Name field for storing persons name and Array of Integers for storing the friend connections. Why Array of integers?
Becouse the next thing that you should do is create an array of TPerson records in order to store pepole records. Once you have this you first populate your pepole array with all available pepole but at this time you still don't fill the information about their friends.
After you have populated your pepole array you go and start entering the fireds information for each person. But instead of storing the friends name you simply store the index at which this friend is stored in pepole array.

So the code for this would look something like this:

//Record used to store information about individual person
TPerson = record
  //Persons name field
  Name: String;
  //Array storing index references to persons friends
  Friends: Array[0..4] of Integer;
end;

//Array storing multiple person records
Pepole: Array[0..4] of TPerson;

This is how you would get name of the first person stored in pepole array:

PersonsName := Pepole[0].Name;

This is how you would get name of the second friend of your first person stored in pepole array

SecondFriendsName := Pepole[Pepole[0].Friends[1]].Name;

This call might be a bit harder to understand.
The code inside the outher square brackets returns the index number of the friend record we are searching for.

Upvotes: 1

Related Questions