Array Manipulations in C#: Part 2

Continued from Part 1.

Copying Array

Copying an array is a common requirement of many applications so the System.Array class provides some useful methods. Let's touch them. 

CopyTo() method

 This method copies the contents of one array into another array given a specific starting index. Let's see a program.

 

    class Program

    {

        static void Main()

        {

 

            int[] x = { 5, 7, 12, 15 };

            int[] cpX = new int[x.Length];

 

            x.CopyTo(cpX, 0);

 

            //print the first array

            Console.WriteLine("Printing first array.");

            for (int i = 0; i < x.Length; i++)

            {

                Console.WriteLine(x[i]);

            }

 

            //print the second array

            Console.WriteLine("Printing second array.");

            for (int i = 0; i < cpX.Length; i++)

            {

                Console.WriteLine(cpX[i]);

            }

            Console.ReadKey();

        }

    }

 

In the above example, (cpX, 0) zero is the starting index of the destination array. 

Array.Copy() method

Another way to copy the array is by using the Array.Copy() method. As in the CopyTo() method, we must initialize the target array before calling this. Let's see a program.

 

    class Program

    {

        static void Main()

        {

 

            int[] x = { 5, 7, 12, 15 };

            int[] cpX = new int[x.Length];

 

            Array.Copy(x,cpX,cpX.Length);

 

            //print the first array

            Console.WriteLine("Printing first array.");

            for (int i = 0; i < x.Length; i++)

            {

                Console.WriteLine(x[i]);

            }

 

            //print the second array

            Console.WriteLine("Printing second array.");

            for (int i = 0; i < cpX.Length; i++)

            {

                Console.WriteLine(cpX[i]);

            }

            Console.ReadKey();

        }

    } 

Multidimentional Arrays

 From my point of view there are two types of multidimentional arrays; both types have a use. Let's talk about them.

(i) Rectangular Array

 

The rectangular array is an array of multiple dimensions and each row is of the same length. Let's see a sample of this.

 

    class Program

    {

        static void Main()

        {

            int[,] recArr = new int[2, 2];

 

            //enter array elements

            Console.WriteLine("Enter the 2x2 array elements.");

            for (int i = 0; i < 2; i++)

            {

                for (int j = 0; j < 2; j++)

                {

                    recArr[i, j] = int.Parse(Console.ReadLine());

                }

            }

 

            //print the array

            Console.WriteLine("Printing array.");

            for (int i = 0; i < 2; i++)

            {

                for (int j = 0; j < 2; j++)

                {

                    Console.WriteLine(recArr[i, j]);

                }

            }

            Console.ReadKey();

        }

    }

 

(ii) Jagged Array

 

A Jagged Array contains some number of inner arrays, each of which may have a unique size. Let's see a sample of this.

 

    class Program

    {

 

        public static int[][] JArr = new int[3][];

 

        static void Main()

        {

            int a, b, c, sum = 0;

 

            Console.WriteLine("Enter the size for 3 inner arrays.");

 

            a = int.Parse(Console.ReadLine());

            b = int.Parse(Console.ReadLine());

            c = int.Parse(Console.ReadLine());

 

            JArr[0] = new int[a];

            JArr[1] = new int[b];

            JArr[2] = new int[c];

 

            Console.WriteLine("Enter the array elements.");

 

            for (int i = 0; i < 3; i++)

            {

                for (int j = 0; j < JArr[i].Length; j++)

                {

                    JArr[i][j] = int.Parse(Console.ReadLine());

                    sum = sum + JArr[i][j];

                }

            }

 

            Console.WriteLine("\n\nThe sum = {0}.", sum);

 

            Console.ReadKey();

        }

    }

 

Thanks for reading.

 

HAVE A HAPPY CODING!!