array_types.md - brainchildservices/curriculum GitHub Wiki
Slide 1
Types of array
In C# 3 types of array is..
1. Single dimensional Array:
The one dimensional array or single dimensional array in C# is the simplest type of array that contains only one row for storing data. It has single set of square bracket (“[]”). To declare single dimensional array in C#, you can write the following code.
string[] Books = new string[5];
The array age is a one dimensional array that contains only 5 elements in a single row.
Slide 2
PROGRAMMING EXAMPLE OF ONE/SINGLE DIMENSIONAL ARRAY:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace One_Dimensional_Array
{
class Program
{
static void Main(string[] args)
{
//Declaring single dimensional array
string[] Books = new string[5];
Books[0] = "C#";
Books[1] = "Java";
Books[2] = "VB.NET";
Books[3] = "C++";
Books[4] = "C";
Console.WriteLine("All the element of Books array is:\n\n");
int i = 0;
//Formatting Output
Console.Write("\t1\t2\t3\t4\t5\n\n\t");
for (i = 0; i < 5; i++)
{
Console.Write("{0}\t", Books[i]);
}
Console.ReadLine();
}
}
}
Output
Slide 3
Multi Dimensional Array In C#
The multi-dimensional array in C# is such type of array that contains more than one row to store data on it. The multi-dimensional array is also known as a rectangular array in c sharp because it has the same length of each row. It can be a two-dimensional array or three-dimensional array or more. It contains more than one comma (,) within single rectangular brackets (“[ , , ,]”). To storing and accessing the elements from a multidimensional array, you need to use a nested loop in the program. The following example will help you to figure out the concept of a multidimensional array.
Slide 4
PROGRAMMING EXAMPLE OF MULTIDIMENSIONAL ARRAY IN C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace multi_dimensional_array
{
class Program
{
static void Main(string[] args)
{
int i, j;
//Declaring multi dimensional array
string[,] Books = new string[3, 3];
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
Console.Write("\nEnter Book Name for {0}. Row and {1}. column:\t", i + 1, j + 1);
Books[i, j] = Console.ReadLine();
}
}
Console.WriteLine("\n\n=========================");
Console.WriteLine("All the element of Books array is:\n\n");
//Formatting Output
Console.Write("\t1\t2\t3\n\n");
//outer loop for accessing rows
for (i = 0; i < 3; i++)
{
Console.Write("{0}.\t", i + 1);
//inner or nested loop for accessing column of each row
for (j = 0; j < 3; j++)
{
Console.Write("{0}\t", Books[i, j]);
}
Console.Write("\n");
}
Console.WriteLine("\n\n=========================");
Console.ReadLine();
}
}
}
Slide 4 Downwards
Output
Slide 4 Downwards
In the preceding example, we create a two-dimensional array named Books which size is [3,3]. It means, this array has three rows and each row contains three columns. Each row can be accessed using an outer loop and each column of rows can be accessed using a nested loop inside the outer loop as follow.
for (i = 0; i < 3; i++) //outer loop for accessing rows
{
Console.Write("{0}.\t", i + 1);
//inner or nested loop for accessing column of each row
for (j = 0; j < 3; j++)
{
Console.Write("{0}\t", Books[i,j]);
}
Console.Write("\n");
}
Slide 5
Jagged Arrays
An array whose elements are arrays is known as Jagged arrays it means “array of arrays“. The jagged array elements may be of different dimensions and sizes. Below are the examples to show how to declare, initialize, and access the jagged arrays.
Example:
// C# program to single-dimensional jagged array
// that contains two single-dimensional array
// elements of different sizes.
using System;
namespace geeksforgeeks {
class GFG {
// Main Method
public static void Main()
{
/*----------2D Array---------------*/
// Declare the array of two elements:
int[][] arr = new int[2][];
// Initialize the elements:
arr[0] = new int[5] { 1, 3, 5, 7, 9 };
arr[1] = new int[4] { 2, 4, 6, 8 };
// Another way of Declare and
// Initialize of elements
int[][] arr1 = { new int[] { 1, 3, 5, 7, 9 },
new int[] { 2, 4, 6, 8 } };
// Display the array elements:
for (int i = 0; i < arr.Length; i++)
{
System.Console.Write("Element [" + i + "] Array: ");
for (int j = 0; j < arr[i].Length; j++)
Console.Write(arr[i][j] + " ");
Console.WriteLine();
}
Console.WriteLine("Another Array");
// Display the another array elements:
for (int i = 0; i < arr1.Length; i++)
{
System.Console.Write("Element [" + i + "] Array: ");
for (int j = 0; j < arr1[i].Length; j++)
Console.Write(arr1[i][j] + " ");
Console.WriteLine();
}
}
}
}
Output :
Element [0] Array: 1 3 5 7 9
Element [1] Array: 2 4 6 8
Another Array
Element [0] Array: 1 3 5 7 9
Element [1] Array: 2 4 6 8
It’s possible to mix jagged and multidimensional arrays. The jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null.