Description |
The current array is recreated in its entirety as a new array. It has the same dimensions, data type and element values.
When the current array holds reference (non primitive) data types, the cloned array element values still refer to the same objects that current array elements refer to. This is what is referred to as a shallow copy. A deep copy would create new versions of the referred objects.
|
| Notes | You must cast the Object output to System.Array when assigning to the target array type.
|
|
Microsoft MSDN Links |
System
System.Array
|
|
|
Cloning a single dimension string array |
program Project1;
{$APPTYPE CONSOLE}
var
myArray, cloneArray : System.Array;
i : Integer;
begin
// Create a 3 element single dimension array of strings
myArray := System.Array.CreateInstance(TypeOf(String), 3);
// Fill the array with values
myArray.SetValue('Hello', 0);
myArray.SetValue('sad', 1);
myArray.SetValue('World', 2);
// Clone the array - this carries out a shallow copy
cloneArray := System.Array(myArray.Clone);
// Display both arrays
for i := 0 to myArray.Length-1 do
Console.WriteLine('myArray[{0}] = {1}',
i.ToString, myArray.GetValue(i));
Console.WriteLine;
for i := 0 to cloneArray.Length-1 do
Console.WriteLine('cloneArray[{0}] = {1}',
i.ToString, cloneArray.GetValue(i));
Console.ReadLine;
end.
| Show full unit code | myArray[0] = Hello
myArray[1] = sad
myArray[2] = World
cloneArray[0] = Hello
cloneArray[1] = sad
cloneArray[2] = World
| | Illustrating the shallowness of the Clone method | program Project1;
{$APPTYPE CONSOLE}
type
MyClass = Class
private
mcName : String;
published
Property Name : String
read mcName
write mcName;
Constructor Create(Name : String); overload;
Function ToString : String; overload;
end;
// MyClass methods
constructor MyClass.Create(Name : String);
begin
inherited Create;
mcName := Name;
end;
function MyClass.ToString : String;
begin
Result := Name;
end;
// Main code
var
myObject : MyClass;
myArray, cloneArray : System.Array;
i : Integer;
begin
// Create a 3 element single dimension array of MyClass objects
myArray := System.Array.CreateInstance(TypeOf(MyClass), 3);
// Fill the array with values
myArray.SetValue(MyClass.Create('Hello'), 0);
myArray.SetValue(MyClass.Create('sad'), 1);
myArray.SetValue(MyClass.Create('World'), 2);
// Clone the array - this carries out a shallow copy
cloneArray := System.Array(myArray.Clone);
// Display both arrays
for i := 0 to myArray.Length-1 do // Arrays are always 0 based
begin
myObject := MyClass(myArray.GetValue(i));
Console.WriteLine('myArray[{0}] = {1}',
i.ToString, myObject.ToString);
end;
Console.WriteLine;
for i := 0 to cloneArray.Length-1 do
begin
myObject := MyClass(cloneArray.GetValue(i));
Console.WriteLine('cloneArray[{0}] = {1}',
i.ToString, myObject.ToString);
end;
// Demonstrate the shallowness of the clone process
Console.WriteLine;
myObject := MyClass(cloneArray.GetValue(1));
myObject.Name := 'wonderful'; // Was 'sad'
Console.WriteLine('After updating just the cloneArray :');
Console.WriteLine;
// Display both arrays again
for i := 0 to myArray.Length-1 do
begin
myObject := MyClass(myArray.GetValue(i));
Console.WriteLine('myArray[{0}] = {1}',
i.ToString, myObject.ToString);
end;
Console.WriteLine;
for i := 0 to cloneArray.Length-1 do
begin
myObject := MyClass(cloneArray.GetValue(i));
Console.WriteLine('cloneArray[{0}] = {1}',
i.ToString, myObject.ToString);
end;
Console.ReadLine;
end.
| Show full unit code | myArray[0] = Hello
myArray[1] = sad
myArray[2] = World
cloneArray[0] = Hello
cloneArray[1] = sad
cloneArray[2] = World
After updating just the cloneArray :
myArray[0] = Hello
myArray[1] = wonderful
myArray[2] = World
cloneArray[0] = Hello
cloneArray[1] = wonderful
cloneArray[2] = World
|
|
|
|