SIGN UP MEMBER LOGIN:    
ARTICLE

Array Manipulations in C#: Part 2

Posted by Abhimanyu Kumar Vatsa Articles | C# Language July 26, 2011
In this article you will learn array manipulations in C#.
Reader Level:

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!!

Login to add your contents and source code to this article
share this article :
post comment
 

Nice Article

Posted by Angelina Erin Jul 27, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor