Translate

Tuesday 30 May 2017

Array Tutorials In C#

Q) What is an array?
Ans) An array is collection of elements of same type. Array consists of Contiguous memory location.
Example:
Let say you have an array “int number[]” of size n;
Then array index will be from 0 to n-1.
Number[0] to Number[n-1]
Q) How to declare an array?
Ans) In C# we declare array as follows:
Datatype[] name_of_array;  : Correct syntax
Datatype name_of_array[] : Incorrect Syntax

Example :
Code 1:
using System;

public class Test
{
 public static void Main()
 {
     int arr[];
     
 }
}

Output: 
prog.cs(7,12): error CS1525: Unexpected symbol `[', expecting `,', `;', or `='
Compilation failed: 1 error(s), 0 warnings

Code 2:
using System;

public class Test
{
 public static void Main()
 {
     int[] arr;
     
 }
}

OutPut: Compiled successfully.
Q) Initialize the array?
Ans):
Let me write one line :
Int[] arr;
What this line represent?
Ans: It will declare an array but it will not initialize the array into memory.

Int[] arr = new int[10];
This line will initialize array using new keyword. Because array is reference type so new keyword is used to create instance of array.
Q) How to assign values to array?
Ans:) there are many ways to assign values of an array
Method 1:
Int[] number={1,2,3,4,5,6,67};
Method 2:
Int[] number = new int[] {1,2,3,4,5,6};

Method 3:
Int[] number = new int[5] {1,2,3,4,5};
Method 4:
Int[] number= new int[2];
number[0]=1;
number[1]=2;

Let me explain all these methods with the help of one example:
using System;

public class Test
{
 public static void Main()
 {
     //int[] arr = new int[3];
     int[] arr= new int[3];
     arr[0]=1;
     arr[1]=2;
     arr[2]=3;
     
     for(int i=0;i<3;i++){
        Console.Write(arr[i]+" ");
     }
          Console.WriteLine();
     
     //int[] arr={4,5,6};
     int[] arr_one={4,5,6};
     for(int i=0;i<3;i++){
        Console.Write(arr_one[i]+" ");
     }
     
     Console.WriteLine();
     
     //int[] arr=new int[]{3,5,7};
     int[] arr_two=new int[]{3,5,7};
     for(int i=0;i<3;i++){
        Console.Write(arr_two[i]+" ");
     }
     
     Console.WriteLine();
     
     //int[] arr=new int[]{3,5,7};
     int[] arr_three=new int[3]{3,5,8};
     for(int i=0;i<3;i++){
        Console.Write(arr_three[i]+" ");
     }
     
     Console.WriteLine();
 }
}
Output:
1 2 3 
4 5 6 
3 5 7 
3 5 8
C# array Types:
1. Multidimensional array
2. Jagged Arrays

MultiDimensional Array : array in which we have a matrix of size m*n, where m is the number of rows and n is the number of columns.

Let me explain with the help of below code:

using System;

public class Test
{
 public static void Main()
 {
     int[,] arr =new int[3,4];
     
     for(int i=0;i<3;i++){
         for(int j=0;j<4;j++){
             arr[i,j]=i+j;
         }
     }
     
     for(int i=0;i<3;i++){
         for(int j=0;j<4;j++){
             Console.Write(arr[i,j]+" ");
         }
         Console.WriteLine();
     }

 }
}
Output:

0 1 2 3 
1 2 3 4 
2 3 4 5

Explanation:
1) Line : int[,] arr =new int[3,4];
It will declare and initialize the array in memory.
It means that we declare an two array having 3 number of rows and 4 number of columns.


Let me explain with the help of one more code:

using System;

public class Test
{
 public static void Main()
 {
     int[,] arr =new int[3,4]{{1,2,3,4},{5,6,7,8},{9,10,11,12}};
  
     for(int i=0;i<3;i++){
         for(int j=0;j<4;j++){
             Console.Write(arr[i,j]+" ");
         }
         Console.WriteLine();
     }

 }
}

Output:
1 2 3 4 
5 6 7 8 
9 10 11 12

Explanation:
In this example we are initializing the 2D array of size 3*4
Jagged Arrays
Array in which the number of columns in each row may vary.
Let me explain with the help of below code:

using System;

public class Test
{
 public static void Main()
 {
     int[][] arr =new int[3][];
     for(int i=0;i<3;i++){
         arr[i]= new int[4];
     }
     
     for(int i=0;i<3;i++){
         for(int j=0;j<4;j++){
             arr[i][j]=i+j;
         }
     }
     
     for(int i=0;i<3;i++){
         for(int j=0;j<4;j++){
             Console.Write(arr[i][j]+" ");
         }
         Console.WriteLine();
     }
 }
}

Output:

0 1 2 3 
1 2 3 4 
2 3 4 5

Explanation:
1) In the above example we can change the number of column based on our requirement.
2) But here we are taking number of column as 4 for all three rows. Let  me explain one more code which has different columns based on rows.
ode:
using System;

public class Test
{
 public static void Main()
 {
     int[][] arr =new int[3][];
     for(int i=0;i<3;i++){
         arr[i]= new int[i+1];
     }
     
     for(int i=0;i<3;i++){
         for(int j=0;j<=i;j++){
             arr[i][j]=i+j;
         }
     }
     
     for(int i=0;i<3;i++){
         for(int j=0;j<=i;j++){
             Console.Write(arr[i][j]+" ");
         }
         Console.WriteLine();
     }

 }
}

Output:
0 
1 2 
2 3 4

Explanation:
So here we can see that every row has different number of columns.

If still you have any doubt please comment below.

No comments:

Working With Java Collections