Array Manipulations in C# - Part 2


Copying Array

Copying array is common requirement of many applications so 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 above example, (cpX, 0) zero is starting index of destination array.

Array.Copy() method

One another way to copy the array is by using Array.Copy() method. As in 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 has its own use. Let's talk for them.

(i) Rectangular Array

The rectangular array is an array of multiple dimensions and each row is of same length. Let's see a program on 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

Jagged Array contains some number of inner arrays, each of which may have unique size. Let's see a program on 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.

Comments

Popular posts from this blog

Customize User's Profile in ASP.NET Identity System

Lambda two tables and three tables inner join code samples