arrays
Mohamed jahid ameer.s
m.sc(software enigineering)
array
 An array stores a fixed-size sequential collection of elements of the same type. An array is
used to store a collection of data, but it is often more useful to think of an array as a
collection of variables of the same type.
 All arrays consist of contiguous memory locations. The lowest address corresponds to the
first element and the highest address to the last element.
Types of array
 single dimensional array
 Multi dimensional array
 Jagged array
Examples:
class Program
{
static void Main(string[] args)
{
string[] programingLanguages = new string[5];
programingLanguages[0] = "C#";
programingLanguages[1] = "Asp.net";
programingLanguages[2] = "Javascript";
programingLanguages[3] = "Php";
programingLanguages[4] = "Java";
foreach (string displayLanguageNames in programingLanguages)
{
Console.WriteLine(displayLanguageNames);
}
Console.ReadKey();
}
}
Output:
C#
Asp.net
Javascript
Php
Java
Single dimensional array
Syntax:
int[] array = new int[5];
This array contains the elements from array[0] to array[4].
The new operator is used to create the array and initialize the array
elements to their default values. In this example, all the array elements
are initialized to zero.
Multi dimensional array
 The simplest form of the multidimensional array is the 2-dimensional array. A
2-dimensional array is a list of one-dimensional arrays.
 A 2-dimensional array can be thought of as a table, which has x number of
rows and y number of columns.
Syantax:
int val = a[2,3];
Example:namespace ArrayApplication
{
class MyArray
{
static void Main(string[] args)
{
/* an array with 5 rows and 2 columns*/
int[,] a = new int[5, 2] {{0,0}, {1,2}, {2,4}, {3,6}, {4,8} };
int i, j;
/* output each array element's value */
for (i = 0; i < 5; i++)
{ for (j = 0; j < 2; j++)
{ Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);
}
} Console.ReadKey();
}
}
}
Output:
a[0,0]: 0
a[0,1]: 0
a[1,0]: 1
a[1,1]: 2
a[2,0]: 2
a[2,1]: 4
a[3,0]: 3
a[3,1]: 6
a[4,0]: 4
a[4,1]: 8
Jagged array
A Jagged array is an array of arrays. You can declare a jagged array
named scores of type int as
Syantax:
int[][] scores = new int[2][]{new int[]{92,93,94},new int[]{85,66,87,88}};
Example:
using System;
namespace jagged_array
{
class Program
{
static void Main(string[] args)
{ int[][] jaggedArray = new int[2][]:
jaggedArray[0] = new int[2] { 7, 9 };
jaggedArray[1] = new int[4] { 12, 42, 26, 38 };
for (int i = 0; i < jaggedArray.Length; i++)
{
System.Console.Write("Element({0}): ", i + 1);
for (int j = 0; j < jaggedArray[i].Length; j++)
{
System.Console.Write(jaggedArray[i][j] + "t");
}
System.Console.WriteLine();
}
Console.ReadLine() }
}}
output
Elements(1): 7, 9
Elements(2): 12, 42, 26, 38

Arrays

  • 1.
  • 2.
    array  An arraystores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.  All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
  • 3.
    Types of array single dimensional array  Multi dimensional array  Jagged array
  • 4.
    Examples: class Program { static voidMain(string[] args) { string[] programingLanguages = new string[5]; programingLanguages[0] = "C#"; programingLanguages[1] = "Asp.net"; programingLanguages[2] = "Javascript"; programingLanguages[3] = "Php"; programingLanguages[4] = "Java"; foreach (string displayLanguageNames in programingLanguages) { Console.WriteLine(displayLanguageNames); } Console.ReadKey(); } }
  • 5.
  • 6.
    Single dimensional array Syntax: int[]array = new int[5]; This array contains the elements from array[0] to array[4]. The new operator is used to create the array and initialize the array elements to their default values. In this example, all the array elements are initialized to zero.
  • 7.
    Multi dimensional array The simplest form of the multidimensional array is the 2-dimensional array. A 2-dimensional array is a list of one-dimensional arrays.  A 2-dimensional array can be thought of as a table, which has x number of rows and y number of columns. Syantax: int val = a[2,3];
  • 8.
    Example:namespace ArrayApplication { class MyArray { staticvoid Main(string[] args) { /* an array with 5 rows and 2 columns*/ int[,] a = new int[5, 2] {{0,0}, {1,2}, {2,4}, {3,6}, {4,8} }; int i, j; /* output each array element's value */ for (i = 0; i < 5; i++) { for (j = 0; j < 2; j++) { Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]); } } Console.ReadKey(); } } }
  • 9.
    Output: a[0,0]: 0 a[0,1]: 0 a[1,0]:1 a[1,1]: 2 a[2,0]: 2 a[2,1]: 4 a[3,0]: 3 a[3,1]: 6 a[4,0]: 4 a[4,1]: 8
  • 10.
    Jagged array A Jaggedarray is an array of arrays. You can declare a jagged array named scores of type int as Syantax: int[][] scores = new int[2][]{new int[]{92,93,94},new int[]{85,66,87,88}};
  • 11.
    Example: using System; namespace jagged_array { classProgram { static void Main(string[] args) { int[][] jaggedArray = new int[2][]: jaggedArray[0] = new int[2] { 7, 9 }; jaggedArray[1] = new int[4] { 12, 42, 26, 38 }; for (int i = 0; i < jaggedArray.Length; i++) { System.Console.Write("Element({0}): ", i + 1); for (int j = 0; j < jaggedArray[i].Length; j++) { System.Console.Write(jaggedArray[i][j] + "t"); } System.Console.WriteLine(); } Console.ReadLine() } }}
  • 12.