as we have
seen earlier, an array is a sequential collection of elements of similar data
types. In c# , an array is an object and thus a reference type, and therefore
they are stored in heap.
Single
dimensional array
// int[] num = new int[5]{1,2,3,4,5};
int[] num = new int[5];
num[0] = 12;
num[1] = 34;
num[2] = 31;
num[3] = 64;
num[4] = 87;
foreach (int j in num)
{
listBox1.Items.Add(j);
}
}
Multidimensional
array and jagged array
A
muntidimensional array is one in which each element of the array is an array
itself.
Rectangular
array( one in which each row contains an equal of columns)
Jagged
array(one in which each row does not necessarily contain an equal number of
element
int[,] mytable =
new int[2, 3];
//int[,] numbers = {{0, 2}, {4, 6}, {8, 10}, {12, 17}, {16,
18}};
Or
mytable[0, 0] = 23;
mytable[0, 1] = 21;
mytable[0, 2] = 53;
mytable[1, 0] = 65;
mytable[1, 1] = 12;
mytable[1, 2] = 25;
foreach (int i in mytable)
{
listBox1.Items.Add(i);
}
Instantiating
and accessing jagged array
A jagged aray is one in which the length of
each row is not the same. For example we may wish to create a table with 3 rows
where the length of first row is 3, second row is 5 and third row is 2, we can instantiate this
jagged array like this:
int[][] mytable
= new int[3][];
mytable[0] = new int[3];
mytable[1] = new int[5];
mytable[2] = new int[2];
mytable[0][0] = 3;
mytable[0][1] = 23;
mytable[1][0] = 1;
mytable[1][1] = 35;
mytable[1][2] = 36;
mytable[1][3] = 13;
mytable[1][4] = 64;
mytable[2][0] = 98;
mytable[2][1] = 76;
foreach (int[] row in mytable)
{
foreach (int col in row)
{
listBox1.Items.Add(col);
}
}
No comments:
Post a Comment