Arrays are the best and important topic for every programming developer take the help of arrays.Arrays are used to store multiple values at a time and in these we are providing how to store he values in single dimentional array and what is the syntax of single dimentional array and give a detailed explanation with examples and what is the use of multi Dimentional array and syntax ,examples and jagged array it is very helpful to write critical logical programs.
Arrays detailed explanation with examples
- Array is a user defined reference type datatype which is used to store multiple values of same datatype in a single variable.
- Array is used hold multiple values of same data type temporarly until the program is running type temporarly until the program is running.
- Array is used to allocate the memory in continuous memory location that is side by side..
- Always the index of array start fro 0 to size-1..
Types Of Arrays
1.Single dimentional arrays
2.Multi dimentional Arrays
3.Jagged Array
Single Dimentional Array
It is used to store homogeneous values in a single variable.
Datatype[] arrayname=new Datatype[arraysize]
Syntax to store the value in the Array
arrayname[index]=value;
Syntax to display the value from the Array
arrayname[index]
Example
class A
{
static void main()
{
int[] ar=new int[5];
Ar[0]=10;
Ar[1]=20;
Ar[2]=30;
Ar[3]=40;
Ar[4]=50;
for(int i=0;i<=4;i++)
{
Console.WriteLine(Ar[i]);
}
}
Multi Dimentional Array
Array is used display the data in matrix format
Datatype[,] Arrayname=new Datatype[noofrows,noofcolums];
Syntax to store the value in the array
Arrayname[rowindex,columnindex]=value;
Example
using system;
class A
{
static void main()
{
int[,] Ar=new int[2,3];
Ar[0,0]=10;
Ar[0,1]=20;
Ar[0,2]=30;
Ar[1,0]=40;
Ar[1,1]=50;
Ar[1,2]=60;
for(int i=0;i<=1;i++)
{
for(int j=0;j<=2;j++)
{
Console.WriteLine(Ar[j,i]);
}
Console.WriteLine();
}
}
Syntax to Declare Multidimentiona Array
Datatype[,] Arrayname=new Datatype[noofrows,noofcolumns]{{row1values},{row2values}};
Example
string[,] Ar=new String[2,3]{{"c","c++","java"},{"mvc","c#","SQL"}};
for(int r=0;r<=1;r++)
{
for(int c=0;c<=2;c++)
{
Console.WriteLine(r,c);
}
Console.WriteLine();
}
Jagged Array
Array inside another Array is Jagged Array
Syntax
Datatype[][] Arrayname=new Datatype[noofrows][];
Example
using system;
class A
{
static void main()
{
int[][] Ar=new int[4][];
Ar[0]=new int[1]{10};
Ar[1]=new int[2]{20.30};
Ar[2]=new int[3]{40,50,60}
Ar[4]=new int[4]{70,80,90,100};
for(int r=0;r<=3;r++)
{
for(int c=0;c<=Ar[i].length-1;c++)
{
Console.WriteLine(Ar[r][c]+"\t");
}
Console.WriteLine();
}
}
}
Friends you like My Post Please Share to Your Friends through Social Media(Facebook,Twitter,Google+).You Feel free comment on my post.
0 comments:
Post a Comment