Home  |  Delphi .net Home  |  System.Array  |  Copy Method
  
Copy  
Method  
Copies a sequence of elements from one array to another
Array Class
System NameSpace
NotCF1.  Procedure Copy ( SourceArray : System.Array; TargetArray : System.Array; Count : Integer ) ; Static;
NotCF2.  Procedure Copy ( SourceArray : System.Array; TargetArray : System.Array; Count : Int64 ) ; Static;
CF3.  Procedure Copy ( SourceArray : System.Array; SourceIndex : Integer; TargetArray : System.Array; Count : Integer; TargetIndex : Integer ) ; Static;
NotCF4.  Procedure Copy ( SourceArray : System.Array; SourceIndex : Int64; TargetArray : System.Array; Count : Int64; TargetIndex : Int64 ) ; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
Copies a Count of elements from SourceArray to TargetArray. The copy starts from the beginning of the source array unless the optional SourceIndex is specified. The copy copies to the start of the target array unles the optional TargetIndex is specified.
 
This is quite different from the CopyTo method which works on single dimension arrays.
 
The Copy method works on arrays of any number of dimensions. It treats all arrays as a long sequence of elements. Here is how a 2 dimensional array of 3 by 4 elements would be processed :
 
Element 0,0
Element 0,1
Element 0,2
Element 0,3
Element 1,0
Element 1,1
Element 1,2
Element 1,3
Element 2,0
Element 2,1
Element 2,2
Element 2,3

 
Copy requires both the SourceArray and TargetArray's to have the same number of dimensions.
Microsoft MSDN Links
System
System.Array
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
Copying from the middle of one array to the end of another
program Project1;
{$APPTYPE CONSOLE}

var
  array1  : System.Array;       // .Net array
  array2  : System.Array;
  lengths : Array of Integer;   // Native Delphi dynamic array
  value   : Integer;
  i0, i1  : Integer;

begin
  // First, we define the array that holds the lengths of each
  // dimension of our multi-dimensional arrays
  SetLength(lengths, 2);  // Dynamic arrays are always 0 based

  lengths[0] := 2;
  lengths[1] := 3;

  // Create 2 multi dimensional arrays of integers
  array1 := System.Array.CreateInstance(TypeOf(Integer), lengths);
  array2 := System.Array.CreateInstance(TypeOf(Integer), lengths);

  // Fill the first array with values
  for i0 := 0 to array1.GetUpperBound(0) do
    for i1 := 0 to array1.GetUpperBound(1) do
      begin
        // Create an element value according to its array position
        value := (i0 + 1) *  (i1 + 1);
        array1.SetValue(TObject(value), i0, i1);
      end;

  // Copy the middle 2 elements to the end of the target array
  System.Array.Copy(array1, 2, array2, 4, 2);

  // Display the first array contents
  for i0 := 0 to array1.GetUpperBound(0) do
    for i1 := 0 to array1.GetUpperBound(1) do
      begin
        value := Integer(array1.GetValue(i0, i1));
        Console.WriteLine('array1[ ' + i0.ToString + ', '   +
                                       i1.ToString + '] = ' +
                                       value.ToString);
      end;

  Console.WriteLine;

  // Display the second array contents
  for i0 := 0 to array2.GetUpperBound(0) do
    for i1 := 0 to array2.GetUpperBound(1) do
      begin
        value := Integer(array2.GetValue(i0, i1));
        Console.WriteLine('array2[ ' + i0.ToString + ', '   +
                                       i1.ToString + '] = ' +
                                       value.ToString);
      end;

  Console.ReadLine;
end.
   array1[ 0, 0] = 1
   array1[ 0, 1] = 2
   array1[ 0, 2] = 3
   array1[ 1, 0] = 2
   array1[ 1, 1] = 4
   array1[ 1, 2] = 6
  
   array2[ 0, 0] = 0
   array2[ 0, 1] = 0
   array2[ 0, 2] = 0
   array2[ 1, 0] = 0
   array2[ 1, 1] = 3
   array2[ 1, 2] = 2
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author