码迷,mamicode.com
首页 > 数据库 > 详细

Handbook之014:动态数组的复制

时间:2015-10-25 06:10:36      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

动态数组直接 := 赋值给新数组,那么这2个数组还是同一个数组,修改任意一个数组成员值,另外一个数组也跟随着变化。

如果动态数组用Copy赋值,那么则不会跟着随动变化

 

技术分享

代码如下:

unit Unit1;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses
  System.Diagnostics,
  System.Math;

type
  TDays = array of Integer;
//函数定义

procedure ShowStaticArrays();
var
  m_Day: TDays;
  m_Day1, m_Day2: TDays;
  I: Integer;
begin
  SetLength(m_Day, 3);
  for I := Low(m_Day) to High(m_Day) do
  begin
    m_Day[I] := I;
  end;

  m_Day1 := m_Day;     // 会修改原数组结果
  m_Day2 := Copy(m_Day, Low(m_Day), Length(m_Day));// 不会修改原数组结果

  m_Day1[1] := 520;
  m_Day2[2] := 3344;

  for I := Low(m_Day) to High(m_Day) do
  begin
    Form1.Memo1.Lines.Add(m_Day[ + I.ToString + ] :=  + m_Day[I].ToString);
  end;
  for I := Low(m_Day) to High(m_Day) do
  begin
    Form1.Memo1.Lines.Add(m_Day1 [ + I.ToString + ] :=  + m_Day1[I].ToString);
  end;
  for I := Low(m_Day) to High(m_Day) do
  begin
    Form1.Memo1.Lines.Add(m_Day2 [ + I.ToString + ] :=  + m_Day2[I].ToString);
  end;
end;

//计时
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowStaticArrays();
end;

end.

Handbook之014:动态数组的复制

标签:

原文地址:http://www.cnblogs.com/GodPan/p/4908144.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!