Arrays are one of the most useful and powerful tools of programing, they are used everywhere from the mundane task like sorting and searching to the exotic like 3-D graphics and modelling. Lets look at the concept of an array.
If we have 100 similar data or homogeneous data then instead of declaring 100 same type of variables we take an array, it is a collection of values with the same or similar data type.Again if we simplify it a bit further then the best example will be a row of houses in a colony. Lets assume you live on the E-Block of colony or city. But is it enough to say to your friend "Come visit me, my address is E-Block". the answer is no because in E-Block there can be thousands of houses which look alike, therefore you qualify your address with a number. That number identifies your house in that block for e.g E-1209. Same can be said about an array, instead of taking different variables we take one and each cell under this variable has a number as an address called index. This concept is further explained in the diagram below.
C# provides with you a 3 types of array which derive from the framework class System.Array they are Single-Dimensional, Multidimensional or Jagged.If we have 100 similar data or homogeneous data then instead of declaring 100 same type of variables we take an array, it is a collection of values with the same or similar data type.Again if we simplify it a bit further then the best example will be a row of houses in a colony. Lets assume you live on the E-Block of colony or city. But is it enough to say to your friend "Come visit me, my address is E-Block". the answer is no because in E-Block there can be thousands of houses which look alike, therefore you qualify your address with a number. That number identifies your house in that block for e.g E-1209. Same can be said about an array, instead of taking different variables we take one and each cell under this variable has a number as an address called index. This concept is further explained in the diagram below.
Single Dimensional Array
Syntax for declaring an array of this type is
<datatype>[]<variable name> = new <data type>[size];
This can be illustrated in C# by the following example
int[] myArr=new int[10];This array contains the elements from array[0] to array[9]. 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.
If you want you can initialize array at the time of declaration, in this case size specifier is not needed
int[] myArr=new int[]{1,45,67,89,90}; int[] myArr22={1,45,67,89,90};
Multidimensional Array
As mentioned above an array can have more than one dimension. the most popular 2-Dimensional and 3-dimensional array. Most of you will seldom use more than 2 dimensions but you can have as many dimensions as you want
int[,] myArr=new int[5,6]In the above example we declare an array that has 5 rows and 6 columns. Initialization can be done at the time of declaration too
int[,] myArr=new int[,]{{1,2},{44,55},{12,45}};In the above example we declare and initialize an array with 3 rows and 2 columns.
There is another way of doing the above when declaration and initialization need to be separated
int[,] array5; array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
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 of arrays."
After declaring the array we need to initialize every row of that array because they themselves are arrays too
int[][] myJArray = new int[3][]; myJArray[0] = new int[2]; myJArray[1] = new int[3]; myJArray[2] = new int[8]; /*It can also be initialized too myJArray[0] = new int[] { 111, 123, }; myJArray[1] = new int[] { 6, 24, 14 }; myJArray[2] = new int[] { 6, 5,8,9,11,99,44,32 }; */
There is another way of declaring everything in a single line
int[][] myJArray = new int[][] { new int[] {1,3,5,7,9}, new int[] {0,2,4,6}, new int[] {11,22} };
That was all about declaration and initialization but what about the usage? More often than not arrays and loops are used together. The beauty of C# is that you can use for loop, while loop and for..each loop . Let me explain this concept with the help of few example
Sample Program
using System; /** * Author : vblord * Date : 10/03/2011 * Description : this is an example of using an array with the for loop and foreach loop * * */ namespace HelloWorld { class ArrayExample { static void Main(string[] args) { string[] namearray = new string[5]; //ACCEPT INPUT FROM THE USER for(int i=0;i<5;i++) { Console.Write("Enter your name : ); namearray[i]=Console.ReadLine(); } //CLEAR SCREEN AND PRINT THe ARRAY Console.Clear(); foreach (string name in namearray) { Console.WriteLine(name); } Console.ReadKey(false); } } }Always remember arrays can be your friend and greatest asset if used judiciously but it is very easy to waste space and complicate matters by using arrays. So be careful.
This was the introduction to arrays we will go into OOPS in the next chapter.
No comments:
Post a Comment