Professional Documents
Culture Documents
Until now if we want to store multiple values in a variable, it is not possible to store all
of them in a single variable.
But instead of declaring so many individual variables, we can declare one array variable
which is capable of storing multiple values of single datatype.
Array is a user-defined datatype of reference type and is used to store multiple values of
same datatype sequentially in a single variable.
Size of an array once defined cannot be changed and hence it is said to be of fixed size.
Once an array is defined, the values in the array are stored sequentially in the memory
location.
Each and every element of an array is initialized automatically with a default value of
that datatype.
Arr_Fig1
Since arrays are of reference type, these are stored in the heap memory.
Single dimensional array is a linear array which can store the data in a single row
Syntax:
datatype[] ArrayName = new datatype[size];
Here new is an operator which will create the array and initialize all its elements with
their default.
size is the size of the array which is fixed once defined.
for ex:
int[] Numbers = new int[5]; where 5 is the length of the Numbers[] array
In this case the array elements are initialized with default value 0.
5 is the size of the array which is fixed once defined.
Here the Numbers[5] array contains the elements from Numbers[0] to Numbers[4].
For ex:
Numbers[0] = 10;
Numbers[1] = 30;
Numbers[2] = 80;
Numbers[3] = 100;
Numbers[4] = 70;
Here the default value 0 is overwritten by the values with which we have initialized the
array elements.
Inline Initialization
Case 1:
If we have fixed values to store in an array, then we can declare an array as follows:
For ex:
Int[] array1 = new int[4] {3, 6, 8, 2};
Here the size mentioned is 4. If we pass the number of elements less than or greater
than the length or size of array, then it will give you syntax error.
Hence we need to pass same number of elements mentioned in the size of array.
Case 2:
Without specifying number of elements or size of an array we can even declare an array
using inline initialization.
For ex:
Int[] array1 = new int[] {10, 2, 6, 4};
Here we have not specified any size, so we can pass as many elements as we want but
once if we passed, that will be the size of that array and later cannot be changed.
using System;
class program
{
public static void Main()
{
int[] array1 = new int[5];
array1[0] = 10;
array1[1] = 20;
array1[2] = 40;
array1[3] = 60;
char[] array2 = new char[4] {'a', 'n', 'k', 'o'};
float[] array3 = new float[] { 2.13F, 5, 9.198F, 10.0F, 12};
Console.WriteLine("Second element in array1 is {0}", array1[1]);
Console.WriteLine("Fifth element in array1 is {0}", array1[4]);
Console.WriteLine("First element in array2 is {0}", array2[0]);
Console.WriteLine("Third element in array3 is {0}", array3[2]);
Console.ReadLine();
}
}
OUTPUT
Arr_Fig2
Analysis
array1[] is an integer array whose size is 5
Here only 4 elements got initialized
5th element is not initialized
When we try to print second and fifth elements,
As usual second element is array1[1] =20
And fifth element array1[4]=0
Syntax:
For two dimensional array
<datatype>[,] = new <datatype>[size];
Two dimensional array is also said to be a rectangular array and it looks like
Int[,] ar = int[2,3]{{2,3,4}{5,6,7}};
ar[0,0] = 2
ar[0,1] = 3
ar[0,2] = 4
ar[1,0] = 5
ar[1,1] = 6
ar[1,2] = 7
using System;
class program
{
public static void Main()
{
int[,] ar = new int[2, 4] { { 1, 5, 8, 7 }, { 6, 4, 3, 2 } };
Console.WriteLine(ar[0, 0] + " " + ar[0, 1] + " " + ar[0, 2] + " " + ar[0, 3]);
Console.WriteLine(ar[1, 0] + " " + ar[1, 1] + " " + ar[1, 2] + " " + ar[1, 3]);
Console.WriteLine("Fourth element in first row of ar is ar[0,3]: {0}", ar[0, 3]);
Console.WriteLine("Number of elements in first dimension: {0}", ar.GetLength(0));
Console.WriteLine("Number of elements in second dimension: {0}", ar.GetLength(1));
Console.WriteLine("Number of elements in ar: {0}", ar.Length);
Console.WriteLine("Number of dimensions in ar: {0}", ar.Rank);
Console.ReadLine();
}
}
OUTPUT
Arr_Fig3
Analysis
Here we have taken two dimensional integer array having 2 rows and four columns
Apart from printing those elements, we have also found out the length of each
dimension
Length of first dimension ar.GetLength{0} gives output 2
Similarly ar.GetLength{1} is 4
To know the total length of array is ar.Length whose output is 8
And finally the rank, nothing but number of dimensions in the array is ar.Rank which is
obviously 2.
Jagged Array
A Jagged array is an array whose elements are also array i.e., it is an array of arrays.
These element arrays inside a jagged array can be of different dimensions and even can
be of different sizes.
Here size 2 represents the number of arrays i.e., two arrays in JaggedAr[][]
We have to initialize the arrays inside before using the Jagged array
class program
{
public static void Main()
{
string[] NameOfEmployee = new string[2];
NameOfEmployee[0] = "John";
NameOfEmployee[1] = "Winson";
Console.ReadLine();
}
}
OUTPUT
Arr_Fig4
Analysis
John
------
Now JaggedAr[0] is stored in arrayin[]
When j=0,
j<arrayin.Length i.e.,0<3 since JaggedAr[0] is a having three elements
0<3 returns true
So it prints
Bachelors
Winson
----------
arrayin = JaggedAr[1]
j=0 and j<arrayin.Length which is 2 returns true
Enters child loop and prints arrayin[0]
Bachelors
J++ = 2 and 2<2 returns false so comes out of the child loop and executes
Console.WriteLine();