Description |
The current ArrayList is recreated in its entirety as a new array. It has the same size 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.Collections.ArrayList when assigning to the target ArrayList type.
|
|
Microsoft MSDN Links |
System.Collections
System.Collections.ArrayList
|
|
|
Cloning a simple string ArrayList |
program Project1;
{$APPTYPE CONSOLE}
uses
System.Collections;
var
myArray, cloneArray : System.Collections.ArrayList;
i : Integer;
begin
// Create a 3 element array of strings
myArray := ArrayList.Create;
myArray.Add('Hello');
myArray.Add('sad');
myArray.Add('World');
// Clone the array - this carries out a shallow copy
cloneArray := ArrayList(myArray.Clone);
// Display both arrays
for i := 0 to myArray.Count-1 do
Console.WriteLine('myArray[{0}] = {1}',
i.ToString, myArray.Item[i]);
Console.WriteLine;
for i := 0 to cloneArray.Count-1 do
Console.WriteLine('cloneArray[{0}] = {1}',
i.ToString, cloneArray.Item[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}
uses
System.Collections;
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
myArray, cloneArray : System.Collections.ArrayList;
myObject : MyClass;
i : Integer;
begin
// Create a 3 element array of strings
myArray := ArrayList.Create;
myArray.Add(MyClass.Create('Hello'));
myArray.Add(MyClass.Create('sad'));
myArray.Add(MyClass.Create('World'));
// Clone the array - this carries out a shallow copy
cloneArray := ArrayList(myArray.Clone);
// Display both arrays
for i := 0 to myArray.Count-1 do
begin
myObject := MyClass(myArray.Item[i]);
Console.WriteLine('myArray[{0}] = {1}',
i.ToString, myObject.ToString);
end;
Console.WriteLine;
for i := 0 to cloneArray.Count-1 do
begin
myObject := MyClass(cloneArray.Item[i]);
Console.WriteLine('cloneArray[{0}] = {1}',
i.ToString, myObject.ToString);
end;
// Demonstrate the shallowness of the clone process
Console.WriteLine;
myObject := MyClass(cloneArray.Item[1]);
myObject.Name := 'wonderful'; // Was 'sad'
Console.WriteLine('After updating just the cloneArray :');
Console.WriteLine;
// Display both arrays again
for i := 0 to myArray.Count-1 do
begin
myObject := MyClass(myArray.Item[i]);
Console.WriteLine('myArray[{0}] = {1}',
i.ToString, myObject.ToString);
end;
Console.WriteLine;
for i := 0 to cloneArray.Count-1 do
begin
myObject := MyClass(cloneArray.Item[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
|
|
|
|