There is probably no generic best method - it all depends on what you want
to do with the data.  A few examples are:

1.  Use dynamic arrays like you suggested.  This is simple and does not
violate any OO programming methodologies.  Ie., it is OK to use dynamic
arrays with OO.

2.  Use TStringList.  The first string list would hold Q1, Q2, etc.  For
each Q1, Q2, etc, you attach another string list to hold X1, X2, etc.  Eg.

var
  MainList, SubList, SubSubList: TStringList;

begin
  MainList := TStringList.Create;

  SubList := TStringList.Create;
  MainList.AddObject ('Q1', SubList);

  SubSubList := TStringList.Create;
  SubList.AddObject ('X1', SubSubList);

  SubSubList.Add ('F1');
  SubSubList.Add ('F2');

  SubList := TStringList.Create;
  MainList.AddObject ('Q2', SubList);

Remember to free the sublists when you free the main list.

3.  Use the composite pattern.

type
  TItem = class (TObject);

  TContainer = class (TItem)
  private
    FChildren: TList;
  end;

  TData = class (TContiner)
  private
    FData: string;
  end;

4.  Use TCollection

type
  TDataItem = class (TColletionItem)
  private
    FChildren: TCollection;
    FData: string;

  public
    constructor Create (ACollection: TCollection);
    property Children: TCollection read FChildren;
    property Data: string read FData write FData;
  end;

constructor TDataItem.Create (ACollection: TCollection);
begin
  inherited;

  FChildren := TCollection.Create (TDataItem);
end;

to add items...

var
  Root: TCollection;

begin
  Root := TCollection.Create (TDataItem);
  with TDataItem (Root.Add) do begin
    Data := 'Q1';
    with TDataItem (Children.Add) do begin
      Data := 'X1';
      TDataItem (Children.Add).Data := 'F1';
      TDataItem (Children.Add).Data := 'F2';
    end;
  end;

----- Original Message -----
From: [EMAIL PROTECTED]
To: Multiple recipients of list delphi
Sent: Wednesday, August 06, 2003 7:29 AM
Subject: [DUG]: Best methods for representing a 3 dimensional array


Morning all,

I need to code a structure that looks like this:

Q1 --- X1 --- F1
              --- F2
              --- F3
     --- X2 ---- F1

Q2 -- X2 --- F1

i.e. For every Q (highest level), there can be multiple X's. For every X,
there can be multiple F's (lowest level). I want to create this structure
and then be able to traverse it. E.g. Q1, X1, F1, then Q1, X1, F2, etc.
right through the whole structure. Each Q, X, and F, are purely a string.

I thought about using a multidimensional dynamic array and using Delphi help
could probably get this working. However, I'm new to Windows programming (1+
yrs) and want to learn OO. I've written a couple of objects recently, so
thought I might try to tackle this as a Q object which has multiple X
objects inside it, and then multiple F objects inside the X objects.

I've started reading about collections and trying some sample code, but
don't know enough to "create a collection within a collection", or even if
that's a correct concept.

Any comments or suggestions? Would a TObjectList be better? Should I just
stick to an array?

TIA
Dave Jollie
Developer, TOWER NZ IT
(: 09 368 4259
J: 09 306 6801
*: [EMAIL PROTECTED]
.: 46 Parnell Rd, Parnell, Auckland
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

Reply via email to