Friday 9 September 2011

Arrays

when we might need to store multiple values of similar type. Such as names of 100
students in a school. One way to do it is to declare 100 variables and store all the names.
A much more simple and efficient way of storing these variable is using Arrays. An
Array is a memory location that is used to store multiple values.
All the values in an array are of the same type, such as int or string and are referenced by
their index or subscript number, which is the order in which these values are stored in the
array. These values are called the elements of the array.
The number of elements that an array contains is called the length of the array.
In C# all arrays are inherited from the System.Array Class.
Arrays can be single or multidimensional. You can determine the dimensions of an array
by the number of subscripts that are used to identify the position of any array element.
A single dimensional array is identified by only a single subscript and an element in a
two-dimensional array is identified by two subscripts.
Arrays in C# also support the concept of Jagged Arrays.
The dimension has to be declared before using them in a program. The array declaration
comprises the name of the array and the number of elements the array can contain.
The Syntax of single dimension array is as follows.

Datatype [] ArrayName = new DataType[number of elements];
e.g.
string [] studentname = new string [5];
You can assign the values at runtime or even at the design time.
Design time declaration:
Studentname [0]=”Rohan”
Studentname [1]=”Mohan”
…..
Studentname[10]=”Nitin”


All arrays starts with the index of 0 i.e. All arrays are Zero Based. This implies that above
array can store 10 elements. Here 0, is the starting index or the lower bound of the array
and 9 is the upper bound while the length of the array is 10.
Example 1.
We will create a C# Console Application that will accept the names of students in an
single dimension array and display it back.
int value = 0, cnt = 0;
//Accept how many students names to enter
Console.Write("Enter the number of students name to enter:
");
value = System.Int32.Parse(Console.ReadLine()) ;
string[] arrnames = new string [value];
for(cnt = 0; cnt<value;cnt++)
{
Console.Write("Enter the name of student " + (cnt + 1)
+ ":", "Student Name");
arrnames[cnt] = Console.ReadLine();
}
Console.WriteLine("Pulling Values from the Array");
//Display the entered value to the text box
for(cnt = 0; cnt < value; cnt++)
{
Console.WriteLine(arrnames[cnt]);
}


Above example will accept number of names to be entered and will add the names in a
loop and then redisplay it on the Console. Note that we have not written any error
handling code that is left to the reader as an exercise.
The Syntax for multi-dimension arrays is as follows:
Previously we saw how we can store multiple names of students. But, if we want to store
related data of students like first name, middle name, last name. In such situations you
can use multi dimension arrays, such as two-or-three dimension arrays.
Datatype [] ArrayName = new Datatype[number of 1st element,
number of 2nd element,….];
e.g.
string[,] studentdetails = new string [10,2];
Index positions of array elements.
0,0 0,1
1,0 1,1
2,0 2,1
3,0 3,1

10,0 10,1
studentdetails(0,0) = “Manoj”
studentdetails(0,1) = “Malik”
To display “Malik” we need to use the index position of the array and say ,
Studentdetails [0,1].
Example 2.
We will create a C# Console Application, which will accept Student Name, Address and
city name and display it on the Console.
string [,] arrsummary = new string[3, 3];
int i=0, j=0;
//As we wanted just 3 columns we have set it to 2, else if
u want to be two only then while declaring the array make
it (2,2) as the lower index is 0.
for(i = 0;i<=2;i++)
{
for(j = 0;j<=2;j++)
{
Console.WriteLine("Enter the value for " + i + "
row and " + j + " column, Summary");
arrsummary[i, j] = Console.ReadLine();
}
}
Console.WriteLine();
//Display the values in the summary array.
for(i = 0;i<=2;i++)
{
string s = "";
for(j = 0;j<=2;j++)
{
if (s.Equals(""))
{
s = arrsummary[i, j];
}
else
{
s = s + " - " + arrsummary[i, j];
}
}
Console.WriteLine(s);
}
Jagged Arrays
A jagged array is an array whose elements are arrays. The elements of a jagged array can
be of different dimensions and sizes. A jagged array is sometimes called an "array-ofarrays."
//Decalaring a Jagged Array
int[][] myJaggedArray = new int[3][];
//Initialize the elements of the Jagged Array
myJaggedArray[0] = new int[5];
myJaggedArray[1] = new int[4];
myJaggedArray[2] = new int[2];
//Fill array elements with values.
myJaggedArray[0] = new int[] {1,3,5,7,9};
myJaggedArray[1] = new int[] {0,2,4,6};
myJaggedArray[2] = new int[] {11,22};
You can access individual array elements like these examples:
// Assign 33 to the second element of the first array:
myJaggedArray[0][1] = 33;
// get the value of second element of the third array:
int i = myJaggedArray[2][1];
Few Important methods in arrays.

GetUpperBound(), GetLowerBound() are the functions used to get the bound of a
array. These methods of the array class. You can use it with single dimension as well as
for multi-dimensional arrays.
GetLowerBound() is to get the upper limit of an array.
GetUpperBound is to get the lower limit of an array.
e.g.
string[] weeks = {"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"};
MessageBox.Show(weeks.GetUpperBound(0).ToString());
//the above statement returns 6 as the upper bound for the array weeks.
Syntax : arrayname.GetUpperBound/GetLowerBound(dimension)
Dimension refers to the which upper/lower bound should be found, 0 for first, 1 for
second and so on.
e.g.
string [,,] student_details = new string[10,20,15];
int upperlimit = 0;
// This will return 10 for the 1st row element
upperlimit = student_details.GetUpperBound(0);
MessageBox.Show(upperlimit.ToString());
// This will return 20 for the 2nd row of element
upperlimit = student_details.GetUpperBound(1);
MessageBox.Show(upperlimit.ToString());

For all GetLowerBound(dimension) it will return 0 as the base lower bound is zero in
.NET

No comments:

Post a Comment