Delphi 2009 Generic Container Unit (generics.collections) [1]: tlist<t>

Source: Internet
Author: User

Delphi 2009 adds a generic container unit: Generics.collections, along with a generics.defaults unit to support.

Generics.collections contains the following practical classes:
Tlist<t>
Tqueue<t>
Tstack<t>
Tdictionary<tkey,tvalue>
Tobjectlist<t>
Tobjectqueue<t>
Tobjectstack<t>
Tobjectdictionary<tkey,tvalue>


With the above generic container, I am afraid Classes.tlist and Contnrs unit under the TObjectList and other series of containers are only compatible exist.

Since the generics.collections.tlist<t> is generic, it should certainly allow for a list of strings, and this is the test for this example.

If you do not understand generics, you should first look at: http://www.cnblogs.com/del/archive/2008/08/14/1268258.html


This example:


Code files:

UnitUnit1;InterfaceusesWindows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;typeTForm1 =class(Tform)    Memo1:tmemo;    Edit1:tedit;    Button1:tbutton;    Button2:tbutton;    Button3:tbutton;    Button4:tbutton;    Button5:tbutton;    Button6:tbutton;    Button7:tbutton;    Button8:tbutton;    Button9:tbutton; Button10:tbutton;procedureFormcreate (Sender:tobject);procedureFormdestroy (Sender:tobject);procedureEdit1change (Sender:tobject);procedureButton1Click (Sender:tobject);procedureButton2click (Sender:tobject);procedureButton3click (Sender:tobject);procedureButton4click (Sender:tobject);procedureButton5click (Sender:tobject);procedureButton6click (Sender:tobject);procedureButton7click (Sender:tobject);procedureButton8click (Sender:tobject);procedureButton9click (Sender:tobject);procedureButton10click (Sender:tobject);End;varForm1:tform1;Implementation{$R *.DFM}usesGenerics.collections;{Delphi 2009 New Generic Container unit}varlist:tlist<string>;{defines a generic TList class, which specifies the string} to use for theStr:string=' Test ';{Build}procedureTform1.formcreate (Sender:tobject);beginList: = tlist<string.  Create;  Memo1.clear;  Edit1.text: = str; Button1.caption: = Button1.caption +' Show '; Button2.caption: = Button2.caption +' Add '; Button3.caption: = Button3.caption +' Insert '; Button4.caption: = Button4.caption +' Delete 1 '; Button5.caption: = Button5.caption +' Delete 2 '; Button6.caption: = Button6.caption +' Find '; Button7.caption: = Button7.caption +' sort '; Button8.caption: = Button8.caption +' Flip '; Button9.caption: = Button9.caption +' Empty '; Button10.caption: = Button10.caption +' Add array ';End;{Release}procedureTform1.formdestroy (Sender:tobject);beginList.free;End;procedureTform1.edit1change (Sender:tobject);beginifEdit1.text <>"'  Thenstr: = Edit1.text;End;{Show}procedureTform1.button1click (Sender:tobject);varI:integer;beginMemo1.clear; forI: =0  toList.count-1  DoMEMO1.LINES.ADD (List[i]);{List[i] = list.item[i]}Text: = Format (' current total:%d ', [List.count]);End;{Add}procedureTform1.button2click (Sender:tobject);beginList.add (str); Button1.Click;{Refresh Display}End;{INSERT, for example, insert in front}procedureTform1.button3click (Sender:tobject);beginList.insert (0, str); Button1.Click;End;{deleted by ordinal, e.g. delete first Data}procedureTform1.button4click (Sender:tobject);beginList.removeat (0); Button1.Click;End;{Delete based on content, e.g. delete First Data}procedureTform1.button5click (Sender:tobject);varSstring;beginS: = list[0];  List.remove (s); Button1.Click;End;{Find}procedureTform1.button6click (Sender:tobject);varN:integer;beginN: = List.indexof (str);{LastIndexOf is from the back; can also be list.contains (str) to determine if STR} is includedifn =-1  ThenShowMessage (' not found ')ElseSHOWMESSAGEFMT ('%s ' is the first%d ', [STR, n+1]);End;{sort}procedureTform1.button7click (Sender:tobject);beginList.sort; Button1.Click;End;{Flip}procedureTform1.button8click (Sender:tobject);beginList.reverse; Button1.Click;End;{Empty}procedureTform1.button9click (Sender:tobject);beginList.clear; Button1.Click;End;{Add array}procedureTform1.button10click (Sender:tobject);ConstArrArray[0..2] of string= (' CodeGear ',' Delphi ',' a ');beginList.add (' Embarcadero ');  List.addrange (arr); Button1.Click;End;End.

Form file:

ObjectForm1:tform1 left =0Top =0Caption =' Form1 'ClientHeight =201ClientWidth =349Color = Clbtnface Font.charset = Default_charset Font.Color = Clwindowtext Font.height =- OneFont.Name =' Tahoma 'Font.style = [] Oldcreateorder = False OnCreate = formcreate OnDestroy = Formdestroy PixelsPerInch = theTextHeight = -ObjectMemo1:tmemo left =0Top =0Width =121Height =201Align = Alleft Lines.strings = (' Memo1 ') ScrollBars = Ssboth TabOrder =0Explicitheight =206EndObjectButton1:tbutton left =136Top = -Width =BayiHeight = -Caption =' Button1 'TabOrder =1OnClick = Button1ClickEndObjectButton2:tbutton left =127Top = -Width = -Height = -Caption =' Button2 'TabOrder =2OnClick = Button2clickEndObjectButton3:tbutton left =127Top = theWidth = -Height = -Caption =' Button3 'TabOrder =3OnClick = Button3clickEndObjectButton4:tbutton left =127Top =106Width = -Height = -Caption =' Button4 'TabOrder =4OnClick = Button4clickEndObjectButton5:tbutton left =127Top =137Width = -Height = -Caption =' Button5 'TabOrder =5OnClick = Button5clickEndObjectButton6:tbutton left = -Top = -Width = -Height = -Caption =' Button6 'TabOrder =6OnClick = Button6clickEndObjectButton7:tbutton left = -Top = theWidth = -Height = -Caption =' Button7 'TabOrder =7OnClick = Button7clickEndObjectButton8:tbutton left = -Top =106Width = -Height = -Caption =' Button8 'TabOrder =8OnClick = Button8clickEndObjectButton9:tbutton left = -Top =137Width = -Height = -Caption =' Button9 'TabOrder =9OnClick = Button9clickEndObjectEdit1:tedit left = -Top = theWidth = -Height = +TabOrder =TenText =' Edit1 'OnChange = Edit1changeEndObjectButton10:tbutton left =127Top =168Width =214Height = -Caption =' Button10 'TabOrder = OneOnClick = Button10clickEndEnd

Delphi 2009 Generic Container Unit (generics.collections) [1]: tlist<t>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.