Wednesday 19 August 2015

What is an Array? How many types of Arrays and With Examples using C#.Net

Array:
               Array was a set similar type values stores in a sequential order. This are of Three types.
  1. Single Dimensional
  2. Two-Dimensional
  3. Jagged Array
                       In C#.Net Arrays can be declared as Fixed length or Dynamic. Fixed Length arrays can be store a predefined Number of Items. While Size of Dynamic arrays increases as you add new Item's to the array.
  1. Single Dimensional :
                                        These are Arrays which stores the values in the form of row.
Syntax: <type>[]<name>= new <type>[size];

example:   int[] arr= new int[5];
                            or
                   int[] arr;
                   arr= new int[5];
                           or
                  int[] arr= {list of values};

Sample Program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class singledimentionArray
    {
        static void Main(string[] args)
        {
            int[] arr = new int[5];
            arr[0] = 10;
            arr[1] = 20;
            arr[2] = 30;
            arr[3] = 40;
            arr[4] = 50;
            for (int i = 0; i < 5; i++)
             
                Console.Write(""+ arr[i] + " ");
            Console.WriteLine();
            
            Console.WriteLine();


          foreach(int i in arr)                    
                Console.Write(i + " ");       


          Console.ReadLine();         


        }       
    }
}

Output:

       10  20  30 40 50
       10  20  30 40 50
       


2.Two-Dimensional Arrays :
                             This are Arrays which hold the data in the form of rows and columns.

Syntax:  <type>[,]<name>= new <type>[rows,columns];

Examples:  int[,] arr = new int[3,4];
                             or
                     int[,]  arr;
                      arr = new int[2,4];
                               
                               or 
                       int[,] arr ={List of values};

Sample Program for Two-Dimensional Arrays:

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class TwoDArray
    {
        static void Main()
        {
            int a = 5;
            int[,] arr = new int[3, 4];
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    arr[i, j] = a;
                    a += 5;
                }
            }

            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.Write(arr[i, j] + " ");
                    Console.WriteLine();
                }
            }
            Console.ReadLine();

        }
    }
}

output:

              5
              10 
              15
              20
              25
              30
              35
              40
              45
              50
              55
              60


3. Jagged Arrays:

                              These are same as Two-Dimensional array. But here the number columns under each row can varying. But in a two-Dimensional Array the number of columns to each row was fixed. These are also known as Array of Arrays. Because here multiple single Dimensional arrays were combined together to form new arrays.

Syntax: <type>[][] <name> = new <type>[rows][];

Example:    int[][] arr = new int[3][];
                                     
                                      or
                        int[][] arr = {List of values};

Sample example for Jagged Arrays :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class jaggedArrays
    {
        static void Main()
        {
            int[][] arr = new int[4][];
            arr[0] = new int[6];
            arr[1] = new int[4];
            arr[2] = new int[3];
            arr[3] = new int[7];

            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr[i].Length; j++)
                    arr[i][j] = j + 1;
            }

            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr[i].Length; j++)
                    Console.Write(arr[i][j] + " ");
                Console.WriteLine();
            }
            Console.ReadLine();

        }
    }
}

Output :

          1  2  3  4  5  6
          1  2  3  4
          1  2  3
          1  2  3  4  5  6  7

1 comment: