An array represents a collection of elements of a particular type. The elements in an array are always stored in a contiguous block of memory, providing highly efficient access. An array is denoted with square brackets after the element type.
One Dimensional Array: The array which represents one row is called one dimensional array.By default array is one dimensional.
Syntax:
<datatype>[] <array name>=new <datatype>[size]
Example:
char[] name = new char[6]; // Declaring an array of 6 characters
vowels [0] = 'd'; //here Square brackets index the array
vowels [1] = 'o';
vowels [2] = 't';
vowels [3] = 'n';
vowels [4] = 'e';
vowels [5] = 't';
Console.WriteLine (name [1]); // o
This prints “o” because array indexes start at 0. We can use a for loop statement to iterate through each element in the array. The for loop in this example cycles the integer i from 0 to 5
char[] name = new char[] {'d','o','t','n','e','t'};// declaring array
for (int i = 0; i < name.Length; i++)
Console.Write (name [i]); // dotnet
The Length property of an array returns the number of elements in the array. Once an array has been created, its length cannot be changed. .
Default Element Initialization:
Creating an array always preinitializes the elements with default values. The default value for a type is the result of a bitwise zeroing of memory.
For example, consider creating an array of integers. Since int is a value type, this allocates 500 integers
in one contiguous block of memory. The default value for each element will be 0:
int[] a = new int[500];
Console.Write (a[123]); // 0
Multidimensional Array:
Multidimensional array are classified into two types
Click here for more : C#.NET Language Basics
One Dimensional Array: The array which represents one row is called one dimensional array.By default array is one dimensional.
Syntax:
<datatype>[] <array name>=new <datatype>[size]
Example:
char[] name = new char[6]; // Declaring an array of 6 characters
vowels [0] = 'd'; //here Square brackets index the array
vowels [1] = 'o';
vowels [2] = 't';
vowels [3] = 'n';
vowels [4] = 'e';
vowels [5] = 't';
Console.WriteLine (name [1]); // o
This prints “o” because array indexes start at 0. We can use a for loop statement to iterate through each element in the array. The for loop in this example cycles the integer i from 0 to 5
char[] name = new char[] {'d','o','t','n','e','t'};// declaring array
for (int i = 0; i < name.Length; i++)
Console.Write (name [i]); // dotnet
The Length property of an array returns the number of elements in the array. Once an array has been created, its length cannot be changed. .
Default Element Initialization:
Creating an array always preinitializes the elements with default values. The default value for a type is the result of a bitwise zeroing of memory.
For example, consider creating an array of integers. Since int is a value type, this allocates 500 integers
in one contiguous block of memory. The default value for each element will be 0:
int[] a = new int[500];
Console.Write (a[123]); // 0
Multidimensional Array:
Multidimensional array are classified into two types
- Rectangular arrays
- Jagged arrays
Arranging no.of elements in matrix format is called as rectangular array.Rectangular arrays are declared using commas to separate each dimension.
Syntax:
<datatype>[ , ] <arrayname>=new <datatype>[size,size]
Example:
Public static void Main()
{
int [ , ] a=new int[ , ]{{1,2,3,4},{5,6,7,8},{9,10,11,12}};
int i=0;
int j=0;
while(i<3)
{
while(j<4)
{
Console.Write(a[i,j] + " ");
}
}
Console.WriteLine();
j=0;
i++;
}
Jagged Arrays:
- It is a collection of rows which may contain distinct no.of elements.
- Jagged arrays are collection of arrays.
- By using Jagged arrays we can save memory.
Syntax:
<datatype>[][] <arrayname>=new <datatype>[size][]
Example:
Public static void Main()
{
int [][] a =new int[3][]{{1,2},{3,4,5},{6,7,8,9}};//declaring & assigning
//for first row
a[0]=new int[2];
a[0][0]=1;
a[0][1]=2;
//for second row
a[1]=new int[3];
a[1][0]=3;
a[1][1]=4;
a[1][2]=5;
//for third row
a[2]=new int[4];
a[2][0]=6;
a[2][1]=7;
a[2][3]=8;
a[2][4]=9;
for(int i=0;i<a.Length;i++)
{
int[] inarr=a[i]
for(int j=0;j<inarr.Length;j++)
{
Console.WriteLine(inarr[j]+" ");
}
Console.WriteLine();
}
}
inarr: Here inarr is a array reference variable which is holding by or referring by individual arrays.
In the first iteration of above for loop 'inarr' pointing to first row and in second iteration 'inarr' is pointing to second row and in third iteration 'inarr' is pointing to third row.


No comments:
Post a Comment