0

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.

12
  • Yes this is possible. Perhaps with a record that held the person and also their friends. Then an array of those records. Or perhaps a generic TList<T>. Or perhaps some other structure. Kind of depends on what you intend to do with the data structure. Commented Feb 5, 2015 at 18:03
  • 2
    Or a plain old multidimensional array. Commented Feb 5, 2015 at 18:04
  • @KenWhite So, arr[0][0] is the person, and arr[0][1] is the first friend, arr[0][2] the second friend and so on. That doesn't bring out the strong difference between person and friend. Commented Feb 5, 2015 at 18:06
  • 1
    @David: Ah, there's the snark. Maybe we could just use index cards and do away with computers altogether. Commented Feb 5, 2015 at 18:17
  • 1
    You really should be looking at using something like a TDictionary, where you could have a key for each person and a value (an object, for instance) to contain the other data. You can then easily retrieve the data for a person by name, update it or use it, etc. Commented Feb 5, 2015 at 20:23

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Due to the way Delphi stores strings, it is possible that no memory would be wasted due to duplicate names. Ofc this would require all the names to be assigned from a common source.
That is true, but I asumed that at some point in the future the OP might also want to store somethng more than just pepole names in which case using of records would be preferable becouse by controling the record structure you quickly control whant information can be stored.
0

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.

1 Comment

Thanks i like your approach as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.