Home  |  Delphi .net Home  |  System.Collections.ArrayList  |  CopyTo Method
  
CopyTo  
Method  
Copies elements from the ArrayList to a single dimension array
ArrayList Class
System.Collections NameSpace
CF1.  Procedure CopyTo ( TargetArray : System.Array ) ;
CF2.  Procedure CopyTo ( TargetArray : System.Array; TargetIndex : Integer ) ;
CF3.  Procedure CopyTo ( SourceIndex : Integer; TargetArray : System.Array; TargetIndex : Integer; Count : Integer ) ;
CF : Methods with this mark are Compact Framework Compatible
Description
The CopyTo method takes elements from the current ArrayList and overlays these values onto elements of the TargetArray.
 
All ArrayList elements are used unless the optional SourceIndex value is provided, giving the start point for the copy. The overlay starts at the beginning of the TargetArray unless the optional TargetIndex value is provided. In this case, Count elements are overlaid from that target array starting index.
 
Important : the TargetArray must be created in advance - the CopyTo method overlays - it does not build. Also, the target array must have at least the same capacity as the number of elements being overlaid (copied).
 
Important : the TargetArray data type must be able to accomodate all element types in the copy process. If the CopyTo method cannot cast to the target array, then an exception is thrown.
 
Just like Clone, CopyTo performs a shallow copy. When the current ArrayList holds reference (non primitive) data types, the target 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
ArrayLists and Arrays are 0 index based.
Microsoft MSDN Links
System.Collections
System.Collections.ArrayList
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
Copying the whole ArrayList onto an Array
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  MyList  : System.Collections.ArrayList;
  MyArray : System.Array;
  i       : Integer;

begin
  // Create our array list object
  MyList := ArrayList.Create;

  // Fill it
  MyList.Add('Data');
  MyList.Add('from');
  MyList.Add('an');
  MyList.Add('ArrayList');

  // Create the target array
  MyArray := System.Array.CreateInstance(TypeOf(String), 8);

  // And fill it
  MyArray.SetValue('0.', 0);
  MyArray.SetValue('1.', 1);
  MyArray.SetValue('2.', 2);
  MyArray.SetValue('3.', 3);
  MyArray.SetValue('4.', 4);
  MyArray.SetValue('5.', 5);
  MyArray.SetValue('6.', 6);
  MyArray.SetValue('7.', 7);

  // Display the array contents
  for i := 0 to MyArray.Length-1 do
    Console.WriteLine(MyArray.GetValue(i).ToString);

  // Copy to the array
  Console.WriteLine;
  Console.WriteLine('Copying from ArrayList to the array :');
  Console.WriteLine;
  MyList.CopyTo(MyArray);

  // Display the array contents
  for i := 0 to MyArray.Length-1 do
    Console.WriteLine(MyArray.GetValue(i).ToString);

  Console.Readline;
end.
   0.
   1.
   2.
   3.
   4.
   5.
   6.
   7.
  
   Copying from ArrayList to the array :
  
   Data
   from
   an
   ArrayList
   4.
   5.
   6.
   7.
Copying from an ArrayList of different element types
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  MyList  : System.Collections.ArrayList;
  MyArray : System.Array;
  i       : Integer;

begin
  // Create our array list object
  MyList := ArrayList.Create;

  // Fill it
  MyList.Add('Name :');
  MyList.Add('Neil Moffatt');
  MyList.Add('Age :');
  MyList.Add(TObject(47));
  MyList.Add('Birthday :');
  MyList.Add(DateTime.Create(1957, 2, 18));

  // Create the target array : we define a general purpose array
  MyArray := System.Array.CreateInstance(TypeOf(TObject), 6);

  // Copy to the array
  Console.WriteLine('Copying from ArrayList to the array :');
  Console.WriteLine;

  MyList.CopyTo(MyArray);

  // Display the array contents
  for i := 0 to MyArray.Length-1 do
    Console.WriteLine(MyArray.GetValue(i).ToString);

  Console.Readline;
end.
   Copying from ArrayList to the array :
  
   Name :
   Neil Moffatt
   Age :
   47
   Birthday :
   18/02/1957 00:00:00
A partial copy to the middle of the target array
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  MyList  : System.Collections.ArrayList;
  MyArray : System.Array;
  i       : Integer;

begin
  // Create our array list object
  MyList := ArrayList.Create;

  // Fill it
  MyList.Add('Source zero');
  MyList.Add('Source one');
  MyList.Add('Source two');
  MyList.Add('Source three');

  // Create the target array
  MyArray := System.Array.CreateInstance(TypeOf(String), 8);

  // And fill it
  MyArray.SetValue('0.', 0);
  MyArray.SetValue('1.', 1);
  MyArray.SetValue('2.', 2);
  MyArray.SetValue('3.', 3);
  MyArray.SetValue('4.', 4);
  MyArray.SetValue('5.', 5);
  MyArray.SetValue('6.', 6);
  MyArray.SetValue('7.', 7);

  // Copy to the middle of the target array
  Console.WriteLine;
  Console.WriteLine('Copying the middle 2 elements ');
  Console.WriteLine('to the middle of the target array :');
  Console.WriteLine;
  MyList.CopyTo(1, MyArray, 3, 2);

  // Display the array contents
  for i := 0 to MyArray.Length-1 do
    Console.WriteLine(MyArray.GetValue(i).ToString);

  Console.Readline;
end.
   Copying the middle 2 elements
   to the middle of the target array :
  
   0.
   1.
   2.
   Source one
   Source two
   5.
   6.
   7.
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author