11.5. Copying Arrays

Copying one array to another comes in handy for programmers, and all it takes is a simple method call in both C# and Java. The Copy method is built into the System.Array object for C#, and the equivalent Java version, arrayCopy, is built into the Java System object.

Basically, C# has two versions of Array.Copy. These two functions can override each other based on the input parameters. Let's look at the two functions in detail.

The following function copies the specified number of elements from the specified source array to the specified destination array:

Array.Copy(sourceArray, destinationArray, length)

The Array.Copy function accepts three parameters: sourceArray of type System.Array, destinationArray of type System.Array, and finally length of type integer.

The following function copies the specified number of elements from a source array starting at the specified source index to a destination array starting at the specified destination index:

Array.Copy(sourceArray, sourceIndex, destinationArray, destinationIndex, length)

The Array.Copy function accepts five parameters: sourceArray of type System.Array, sourceIndex of type integer, destinationArray of type System.Array, destinationIndex of type integer, and length of type integer.

The next two listings demonstrate how to use the Array.Copy function. In Listing 11.7, the objective is to copy all the elements of array A to array B. For simplicity, we declare and initialize source array A as follows:

int[] A = new int[] {0, 1, 2, 4, 6};

We declare and initialize destination array B like this:

double[] B = new double[5];

Let's copy four elements of source array A to destination array B by calling the built-in copy method of System.Array as follows:

Array.Copy(A, B, 4);

Note that the last element of destination array A is 0, because we copied only the first four elements from array A.

Listing 11.7. CopyArray1.cs: Copying Arrays in C#
class CopyArray1{
  public static void Main(){
    int[] A = new int[] {0, 1, 2, 4, 6};
    double[] B = new double[5];

    Array.Copy(A, B, 4);
    Console.Write("The elements of B are:");
    Console.Write(";bsn");
    for (int i = 0; i < B.Length; i ++)
      Console.Write("{0, 3}", B[i] );
  }
}

The output of Listing 11.7 is as follows:

The elements of B are:
0 1 2 4 0

Now let's look at the second version of the Array.Copy method in C# (Listing 11.8). This is similar to the System.arrayCopy function of Java. We initialize and declare the source array as follows:

int[] A = new int[] {0, 1, 2, 4, 6, 8, 10};

The objective of the listing is to replace the first three elements starting at index 1 (1,2,4) with the first three elements starting at index 0 (0,1,2). We use the following method call:

Array.Copy(A, 1, A, 0, 3);

Listing 11.8. CopyArray2.cs: Copying Arrays in C#, Version 2
class CopyArray2{
  public static void Main(){
  int[] A = new int[] {0, 1, 2, 4, 6, 8, 10};

  Array.Copy(A, 1, A, 0, 3);
  Console.Write("The elements of A are:");
  Console.Write("
");
  for (int i = 0; i < A.Length; i ++)
    Console.Write("{0, 3}", A[i] );
    }
}

The output of Listing 11.8 is as follows:

The elements of A are:
1 2 4 4 6 8 10

The Java version of Array.Copy is almost the same, except for the method name. Listing 11.9 shows the Java version.

Listing 11.9. CopyArray2.Java: Copying Arrays in Java
class CopyArrays2
{
  public static void main(String[] args)
  {
    int[] A = new int[] {0, 1, 2, 4, 6, 8, 10};
    System.arraycopy(A, 1, A, 0, 3);
    System.out.print("The elements of A are:");
    System.out.print(";bsn");
      for (int i = 0; i < A.length; i ++)
        System.out.print(A[i] + " ");
  }
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.184.21