返回

1:TList类 (TObject->TList)

Unit:Classes

简述:TList存储指针数组。

描述:
TList存储指针数组,通常用于维护对象列表。
添加或删除列表中的对象。
重新排列列表中的对象。
找到并访问列表中的对象。
对列表中的对象进行排序。

代码示例:

typedef struct AList

{
int I;
char C;
} TAList;

typedef TAList* PAList;

void __fastcall TForm1::Button1Click(TObject *Sender)

{
PAList AStruct;
TList *MyList = new TList;
// fill the TList
AStruct = new TAList;
AStruct->I = 100;
AStruct->C = 'Z';
MyList->Add(AStruct);
AStruct = new TAList;
AStruct->I = 100;
AStruct->C = 'X';
MyList->Add(AStruct);

// Go through the list, writing the elements to the
// canvas of a paintbox component.
int Y = 10; // position on canvas
for (int i = 0; i < MyList->Count; i++)

{
AStruct = (PAList) MyList->Items[i];
PaintBox1->Canvas->TextOut(10, Y, IntToStr(AStruct->I));
Y += 30; // Increment Y Value again
PaintBox1->Canvas->TextOut(10, Y, AStruct->C);
Y += 30; //Increment Y Value
}

// Clean up ?must free memory for the items as well as the list
for (int i = 0; i < MyList->Count; i++)
{
AStruct = (PAList) MyList->Items[i];
delete AStruct;
}
delete MyList;

}