Array:
Array was a set similar type values stores in a sequential order. This are of Three types.
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();
}
}
}
Array was a set similar type values stores in a sequential order. This are of Three types.
- Single Dimensional
- Two-Dimensional
- Jagged Array
- Single Dimensional :
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.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
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
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 2 3 4 5 6
1 2 3 4
1 2 3
1 2 3 4 5 6 7
Nice post very helpful
ReplyDeletedbakings