----------
开发环境Delphi7
--------------
--------------Unit-开始------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMyArrayOneStr=array of string;
TMyArrayTwoStr=array of array of string;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
FMyArrayOneStr:TMyArrayOneStr;
FMyArrayTwoStr:TMyArrayTwoStr;
function GetMyarrayValueOne(i:Integer):String;
procedure SetMyarrayValueOne(i:Integer ;const Value:string);
function GetMyarrayValueTwo(i, j: integer): string;
procedure SetMyarrayValueTwo(i, j: integer; const Value: string);
{ Private declarations }
public
property MyArrayValueOne[i:integer]:string read GetMyarrayValueOne write SetMyarrayValueOne;
property MyArrayValueTwo[i,j:integer]:string read GetMyarrayValueTwo write SetMyarrayValueTwo;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
function TForm1.GetMyarrayValueOne(i: Integer): String;
begin
Result:=FMyArrayOneStr[i] ;
end;
procedure TForm1.SetMyarrayValueOne(i: Integer; const Value: string);
begin
FMyArrayOneStr[i]:=Value;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i:Integer;
begin
//动态数组申请内存
SetLength(FMyArrayOneStr,10);
SetLength(FMyArrayTwoStr,10);
for i:=Low(FMyArrayTwoStr) to High(FMyArrayTwoStr) do
begin
SetLength(FMyArrayTwoStr[i],10);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
MyArrayValueOne[0]:='AAA';
ShowMessage(MyArrayValueOne[0]);
//ShowMessage(MyArrayValueOne[10]); //10超出数组下标上限,越界问题 会报错 ,仅提供简单的例子,其他的不做讲解
end;
procedure TForm1.FormDestroy(Sender: TObject);
var
i:integer;
begin
//一维数组释放
//FMyArrayOneStr := nil; //数组释放1 //数组等于nil,数组的引用计数变成0会自动释放掉内存
//SetLength(FMyArrayOneStr, 0);//数组释放2
Finalize(FMyArrayOneStr); //数组释放3
//二维数组释放
for i:=Low(FMyArrayTwoStr) to High(FMyArrayTwoStr) do//这里释放好像是多余的
begin
//FMyArrayTwoStr[i]:=nil;
Finalize(FMyArrayTwoStr[i])
//SetLength(FMyArrayTwoStr[i],0);
end;
//下面释放了就好了,上面的循环释放好像没有必要,因为下面的代码就能把内存释放掉,所以循环好像不需要也行,处于谨慎考虑,加上循环也没啥坏处
FMyArrayTwoStr:=nil;
//Finalize(FMyArrayTwoStr);
//SetLength(FMyArrayTwoStr, 0);
end;
function TForm1.GetMyarrayValueTwo(i, j: integer): string;
begin
Result:=FMyArrayTwoStr[i,j] ;
end;
procedure TForm1.SetMyarrayValueTwo(i, j: integer; const Value: string);
begin
FMyArrayTwoStr[i,j]:=Value;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
MyArrayValueTwo[0,0]:='CCC';
ShowMessage(MyArrayValueTwo[0,0]);
//ShowMessage(MyArrayValueTwo[10,10]); //10超出数组下标上限,越界问题 会报错 ,仅提供简单的例子,其他的不做讲解
end;
end.
--------------Unit-结束------
-----------Form开始
object Form1: TForm1
Left = 722
Top = 476
Width = 366
Height = 228
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 80
Top = 56
Width = 193
Height = 25
Caption = '属性之一维数组'
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 80
Top = 112
Width = 193
Height = 25
Caption = '属性之二维数组'
TabOrder = 1
OnClick = Button2Click
end
end
-----------Form结束