Delphi 集合的用法

unit Unit4;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TEnum = (One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten);

TSetEnum = set of TEnum;

TForm4 = class(TForm)

GroupBox1: TGroupBox;

Button1: TButton;

Button2: TButton;

GroupBox2: TGroupBox;

Button3: TButton;

Button4: TButton;

Button5: TButton;

Button6: TButton;

Button7: TButton;

Button8: TButton;

Button9: TButton;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

procedure Button3Click(Sender: TObject);

procedure Button5Click(Sender: TObject);

procedure Button6Click(Sender: TObject);

procedure Button7Click(Sender: TObject);

procedure Button4Click(Sender: TObject);

procedure Button8Click(Sender: TObject);

procedure Button9Click(Sender: TObject);

private

SetEnum, SetEnum1, SetEnum2: TSetEnum ;

{ Private declarations }

public

{ Public declarations }

end;

var

Form4: TForm4;

implementation

{$R *.dfm}

procedure TForm4.Button1Click(Sender: TObject);

var

s1, s2: string;

begin

s1 := 'aaa';

s2 := s1;

s2 := 'bbb';

ShowMessage('字符串S1的值: ' + s1 + ' ; 字符串S2的值: ' + S2);

end;

procedure TForm4.Button2Click(Sender: TObject);

var

A1, A2: array of string;

begin

SetLength(A1, 1);

A1[0] := 'aaa';

A2 := A1;

A2[0] := 'bbb';

ShowMessage('数组A1的值:' + A1[0]+ ' ;数组A2的值:' + A2[0]);

end;

//初始化

procedure TForm4.Button3Click(Sender: TObject);

begin

SetEnum := [one, Two];

SetEnum1 := [Three, Nine, Ten]

end;

//集合减少

procedure TForm4.Button5Click(Sender: TObject);

begin

SetEnum := SetEnum - [Two];

end;

//集合增加

procedure TForm4.Button6Click(Sender: TObject);

begin

SetEnum := SetEnum + [Three];

end;

//集合减少

procedure TForm4.Button7Click(Sender: TObject);

begin

Exclude(SetEnum, One);

end;

//集合增加

procedure TForm4.Button4Click(Sender: TObject);

begin

Include(SetEnum, Four);

Include(SetEnum, Ten);

end;

//执行校验

procedure TForm4.Button8Click(Sender: TObject);

begin

if One in SetEnum then

begin

ShowMessage('Exclude函数执行失败');

end

else if Two in SetEnum then

begin

ShowMessage('SetEnum - [Two]执行失败');

end

else if not (Three in SetEnum) then

begin

ShowMessage('SetEnum + [Three]执行失败');

end

else if not (Four in SetEnum) then

begin

ShowMessage('Include函数执行失败');

end

else if not (ten in SetEnum) then

begin

ShowMessage('Include函数执行失败');

end

else

begin

ShowMessage('执行成功!');

end;

end;

//取得集合的交集

procedure TForm4.Button9Click(Sender: TObject);

begin

SetEnum2 := SetEnum1 * SetEnum;

if Three in SetEnum2 then

begin

ShowMessage('交集成功执行成功');

end;

end;

end.

————————————————————————————————————————————————————————————

————————————————————————————————————————————————————————————

object Form4: TForm4

Left = 359

Top = 186

Width = 558

Height = 418

Caption = 'Form4'

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 GroupBox1: TGroupBox

Left = 24

Top = 16

Width = 209

Height = 145

Caption = '字符串和数组的的应用'

TabOrder = 0

object Button1: TButton

Left = 24

Top = 40

Width = 169

Height = 25

Caption = '字符串的引用'

TabOrder = 0

OnClick = Button1Click

end

object Button2: TButton

Left = 24

Top = 88

Width = 169

Height = 25

Caption = '数组的引用'

TabOrder = 1

OnClick = Button2Click

end

end

object GroupBox2: TGroupBox

Left = 238

Top = 22

Width = 307

Height = 139

Caption = '集合的应用'

TabOrder = 1

object Button3: TButton

Left = 24

Top = 24

Width = 80

Height = 25

Caption = '集合初始化'

TabOrder = 0

OnClick = Button3Click

end

object Button4: TButton

Left = 216

Top = 68

Width = 80

Height = 25

Caption = '集合增加2'

TabOrder = 1

OnClick = Button4Click

end

object Button5: TButton

Left = 112

Top = 28

Width = 80

Height = 25

Caption = '集合减少1'

TabOrder = 2

OnClick = Button5Click

end

object Button6: TButton

Left = 216

Top = 28

Width = 80

Height = 25

Caption = '集合增加1'

TabOrder = 3

OnClick = Button6Click

end

object Button7: TButton

Left = 112

Top = 68

Width = 80

Height = 25

Caption = '集合减少2'

TabOrder = 4

OnClick = Button7Click

end

object Button8: TButton

Left = 24

Top = 72

Width = 81

Height = 25

Caption = '检测集合值'

TabOrder = 5

OnClick = Button8Click

end

object Button9: TButton

Left = 120

Top = 104

Width = 81

Height = 25

Caption = '集合的交集'

TabOrder = 6

OnClick = Button9Click

end

end

end