Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

Use the luxery of TList with Integers (instead of Arrays)

Title: use the luxery of TList with Integers (instead of Arrays) Question: using TList gives you the opportunity to to easy opperations with data like Exchange, Move, Delete, Remove, Add, Insert, First, Last, Sort, Count, .... all of this Array does not support (with the same comforet). Unfortunately enough, TList is used with pointers (eg to Controls) Here is a way to use Integers with a TList instead of with a TArray. So you can work much easier with the integers as you work with TList instead of with arrays Answer: Why not use TList with all its luxurious operators with Integers (should work with Floating point-values too. For Strings, you've got TString to use. TList normally works with pointers (eg for Controls (TEdit, TBitBtn). We can use TList also with Integers!unit Unit1; { using TList gives you the opportunity to to easy opperations with data like Exchange, Move, Delete, Remove, Add, Insert, First, Last, Sort, Count, .... all of this Array does not support (with the same comforet). Unfortunately enough, TList is used with pointers (eg to Controls) Here is a way to use Integers with a TList instead of with a TArray. So you can work much easier with the integers as you work with TList instead of with arrays} interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ComCtrls; type TForm1 = class(TForm) StatusBar1: TStatusBar; GroupBox1: TGroupBox; BitBtn1: TBitBtn; Edit1: TEdit; GroupBox2: TGroupBox; GroupBox3: TGroupBox; GroupBox4: TGroupBox; Memo1: TMemo; Memo2: TMemo; BitBtn2: TBitBtn; procedure BitBtn1Click(Sender: TObject); procedure BitBtn2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; type TMyDataPoint = class public Data1: integer; constructor Create(aData1: integer); end; var Form1: TForm1; implementation {$R *.dfm} constructor TMyDataPoint.Create(aData1: integer); begin Data1 := aData1; end; procedure TForm1.BitBtn1Click(Sender: TObject); var MyList : TList; begin MyList := TList.Create; MyList.Add(TMyDataPoint.Create(5)); Statusbar1.Panels[1].Text := inttostr ( TMyDataPoint(MyList.Items[0]).Data1 ); MyList.Free; end; procedure TForm1.BitBtn2Click(Sender: TObject); var i: integer; MyList : TList; begin MyList := TList.Create; for i:= 0 to Memo1.Lines.Count-1 do MyList.Add(TMyDataPoint.Create( strtoint ( Memo1.Lines[i]))); Memo2.Clear; for i:= 0 to Memo1.Lines.Count-1 do Memo2.Lines.Add ( inttostr( TMyDataPoint(MyList.Items[i]).Data1 )); MyList.Free; end; end. -------------- The dpr-file: program TList_Integer; uses Forms, Unit1 in 'Unit1.pas' {Form1}; {$R *.res} begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end. ------------ the dfm-file: object Form1: TForm1 Left = 374 Top = 470 Width = 404 Height = 252 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object StatusBar1: TStatusBar Left = 0 Top = 206 Width = 396 Height = 19 Panels = item Text = 'Dir:\TList_Integer' Width = 92 end item Width = 50 end item Text = 'use TList with Integer (insted of TArray)' Width = 50 end SimplePanel = False end object GroupBox1: TGroupBox Left = 0 Top = 0 Width = 217 Height = 49 Caption = 'One String only (useless....)' TabOrder = 1 object BitBtn1: TBitBtn Left = 8 Top = 16 Width = 75 Height = 25 Caption = 'BitBtn1' TabOrder = 0 OnClick = BitBtn1Click end object Edit1: TEdit Left = 88 Top = 16 Width = 121 Height = 21 TabOrder = 1 Text = 'Edit1' end end object GroupBox2: TGroupBox Left = 0 Top = 50 Width = 393 Height = 151 Caption = 'Several Strings' TabOrder = 2 object GroupBox3: TGroupBox Left = 8 Top = 16 Width = 185 Height = 105 Caption = 'Read from' TabOrder = 0 object Memo1: TMemo Left = 0 Top = 12 Width = 180 Height = 89 Lines.Strings = ( '2' '32' '7000' '9' '8' '101') TabOrder = 0 end end object GroupBox4: TGroupBox Left = 200 Top = 16 Width = 185 Height = 105 Caption = 'Write into' TabOrder = 1 object Memo2: TMemo Left = 0 Top = 12 Width = 180 Height = 89 Lines.Strings = ( 'Memo2') TabOrder = 0 end end object BitBtn2: TBitBtn Left = 8 Top = 120 Width = 75 Height = 25 Caption = 'BitBtn2' TabOrder = 2 OnClick = BitBtn2Click end end end -----------------