4.1 Declaring and Using Arrays

 <  Day Day Up  >  

You want to create a simple array of objects, and you know exactly how many elements the collection needs.


Technique

Use a single dimension array if you want a collection of objects that can be accessed in a linear fashion. The array data type is the data type of the objects you want to insert into the array, followed by left and right brackets. To initialize the array with a set of values, assign the array variable to a comma-delimited list of values surrounded by left and right braces.

 
 int[] arr1 = {1,2,3,4,5,6,7,8,9,10}; 

If you don't want to initialize the elements in the array but would rather set the individual elements later, use the new keyword on the collection data type, specifying the array size in brackets.

 
 int[] arr2 = new int[10]; 

To access a single element in the array, place the index of the element you want to access ”relative to the beginning of the array element, which has an index of 0 ”in brackets following the array variable name :

 
 static void TestSingleDimensionArray() {     int[] arr1 = {1,2,3,4,5,6,7,8,9,10};     int[] arr2 = new int[10];     for( int i = 0; i < arr1.Length; i++ )     {         arr2[i] = arr1[i];         Console.WriteLine( arr1[i] + "=" + arr2[i] );     } } 

If you want to create an array with more than one dimension, declare the variable using the data type to place in the array, followed by brackets containing a comma to separate each dimension within the array. For instance, a two-dimensional array used to hold data in rows and columns , as shown in the TestMultiDimensionArray listing, would contain one comma. To create the array without initializing the individual elements, use the same technique for a single-dimension array but use a comma-separated list denoting the number of elements each dimension can hold:

 
 static void TestMultiDimensionArray() {     string[,] arr1 = new string[10,10];     // assign values     for( int row = 0; row < 10; row++ )     {         for( int col = 0; col < 10; col++ )         {             arr1[row,col] = row + "," + col;         }     }     // print values     for( int i = 0; i < 10; i++ )     {         for( int j = 0; j < 10; j++ )         {             Console.Write( arr1[i,j] + "\t" );         }         Console.WriteLine();     } } 

To create an initialized multidimensional array, place each dimension's value within left and right braces similar to the initialization of a single-dimension array, and place each dimension's element list within braces also.

 
 int[,] identity = { {0,1},{1,0} }; 

Comments

Arrays are simple collections whose elements you access using indices. Because their use is widespread in a variety of different programming scenarios, C# created special syntactical elements to aid in their creation, initialization, and element access.

The declaration of an array consists of the data type of the elements you want to place into the array followed by a set of brackets and the variable name of the array. These brackets specify how many dimensions an array has. A dimension in an array is the same as its mathematical counterpart . A two-dimensional array, for instance, contains a group of elements that in turn each contain a group of elements. A real-world example is a spreadsheet that contains two dimensions, a row and a column. Although it might be hard to think of dimensions in the fifth or higher degree, you can easily create them in a programming language like C#.

To access an individual element within an array, you use an indexer , which is an integer value between brackets specifying the number or index of the element you want to access. For a single-dimension array, there is one index value per array element. For a multidimensional array, the indices specified within a comma-separated list correspond to the element at each location of the dimension in which the index is placed. For instance, an element within a two-dimensional array uses the format [ x,y ] , where x is an index into the first dimension and y is the index into the second dimension. This line in turn returns the value at that location.

A major advantage to using an array over any other collection type is the fast performance when retrieving an element. When you create an array, the memory is allocated as a single contiguous block of memory, regardless of the number of dimensions within the array. Therefore, given an index value, the Common Language Runtime (CLR) can easily find the corresponding element by multiplying the index by the size of the element's data type and using that value as an offset from the beginning of the array's memory block. The performance of accessing an element within an array given an index value runs in constant or 0(1) time.

Although the performance of element access within an array is certainly fast, it still requires that you know the index of the element you want to access. In some cases, this requirement might be acceptable, but once you start needing features such as finding an element, the performance of using an array drops . Searching an array for a specific value requires that the program access every element, beginning with the first index and continuing until it finds the element or reaches the end of the array.

Due to its simple nature, arrays also have a few more drawbacks. Once you create an array, you cannot add elements past the sizes declared for each dimension. In other words, an array cannot increase in size nor can it insert elements into the middle. Additionally, a single-dimension or multidimension array can consume large amounts of memory, even if you do not place values in the array. For small arrays, this point is a moot, but large multidimensional arrays reserve large portions of memory, even if you don't place values into the array elements.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net