Using the Length Property


A number of benefits result because C# implements arrays as objects. One comes from the fact that each array has associated with it a Length property that contains the number of elements that an array can hold. Thus, each array carries with it a field that contains the array’s length. Here is a program that demonstrates this property:

 // Use the Length array property. using System; class LengthDemo {   public static void Main() {     int[] nums = new int[10];     Console.WriteLine("Length of nums is " + nums.Length);     // use Length to initialize nums     for(int i=0; i < nums.Length; i++)       nums[i] = i * i;     // now use Length to display nums     Console.Write("Here is nums: ");     for(int i=0; i < nums.Length; i++)       Console.Write(nums[i] + " ");     Console.WriteLine();   } }

This program displays the following output:

 Length of nums is 10 Here is nums: 0 1 4 9 16 25 36 49 64 81

In LengthDemo notice the way that nums.Length is used by the for loops to govern the number of iterations that take place. Since each array carries with it its own length, you can use this information rather than manually keeping track of an array’s size. Keep in mind that the value of Length has nothing to do with the number of elements that are actually in use. It contains the number of elements that the array is capable of holding.

When the length of a multidimensional array is obtained, the total number of elements that can be held by the array is returned. For example:

 // Use the Length array property on a 3-D array. using System; class LengthDemo3D {   public static void Main() {     int[,,] nums = new int[10, 5, 6];     Console.WriteLine("Length of nums is " + nums.Length);   } }

The output is shown here:

 Length of nums is 300

As the output verifies, Length obtains the number of elements that nums can hold, which is 300 (10×5×6) in this case. It is not possible to use Length to obtain the length of a specific dimension.

The inclusion of the Length property simplifies many algorithms by making certain types of array operations easier—and safer—to perform. For example, the following program uses Length to reverse the contents of an array by copying it back-to-front into another array:

 // Reverse an array. using System; class RevCopy {   public static void Main() {     int i,j;     int[] nums1 = new int[10];     int[] nums2 = new int[10];     for(i=0; i < nums1.Length; i++) nums1[i] = i;     Console.Write("Original contents: ");     for(i=0; i < nums2.Length; i++)       Console.Write(nums1[i] + " ");     Console.WriteLine();     // reverse copy nums1 to nums2     if(nums2.Length >= nums1.Length) // make sure nums2 is long enough       for(i=0, j=nums1.Length-1; i < nums1.Length; i++, j--)         nums2[j] = nums1[i];     Console.Write("Reversed contents: ");     for(i=0; i < nums2.Length; i++)       Console.Write(nums2[i] + " ");     Console.WriteLine();   } }

Here is the output:

 Original contents: 0 1 2 3 4 5 6 7 8 9 Reversed contents: 9 8 7 6 5 4 3 2 1 0

Here, Length helps perform two important functions. First, it is used to confirm that the target array is large enough to hold the contents of the source array. Second, it provides the termination condition of the for loop that performs the reverse copy. Of course, in this simple example, the size of the arrays is easily known, but this same approach can be applied to a wide range of more challenging situations.

Using Length with Jagged Arrays

A special case occurs when Length is used with jagged arrays. In this situation, it is possible to obtain the length of each individual array. For example, consider the following program, which simulates the CPU activity on a network with four nodes:

 // Demonstrate Length with jagged arrays. using System; class Jagged {   public static void Main() {     int[][] network_nodes = new int[4][];     network_nodes[0] = new int[3];     network_nodes[1] = new int[7];     network_nodes[2] = new int[2];     network_nodes[3] = new int[5];     int i, j;     // fabricate some fake CPU usage data     for(i=0; i < network_nodes.Length; i++)       for(j=0; j < network_nodes[i].Length; j++)         network_nodes[i][j] = i * j + 70;     Console.WriteLine("Total number of network nodes: " +                       network_nodes.Length + "\n");     for(i=0; i < network_nodes.Length; i++) {       for(j=0; j < network_nodes[i].Length; j++) {         Console.Write("CPU usage at node " + i +                       " CPU " + j + ": ");         Console.Write(network_nodes[i][j] + "% ");         Console.WriteLine();       }       Console.WriteLine();     }   } }

The output is shown here:

 Total number of network nodes: 4 CPU usage at node 0 CPU 0: 70% CPU usage at node 0 CPU 1: 70% CPU usage at node 0 CPU 2: 70% CPU usage at node 1 CPU 0: 70% CPU usage at node 1 CPU 1: 71% CPU usage at node 1 CPU 2: 72% CPU usage at node 1 CPU 3: 73% CPU usage at node 1 CPU 4: 74% CPU usage at node 1 CPU 5: 75% CPU usage at node 1 CPU 6: 76% CPU usage at node 2 CPU 0: 70% CPU usage at node 2 CPU 1: 72% CPU usage at node 3 CPU 0: 70% CPU usage at node 3 CPU 1: 73% CPU usage at node 3 CPU 2: 76% CPU usage at node 3 CPU 3: 79% CPU usage at node 3 CPU 4: 82%

Pay special attention to the way Length is used on the jagged array network_nodes. Recall, a two-dimensional jagged array is an array of arrays. Thus, when the expression

 network_nodes.Length

is used, it obtains the number of arrays stored in network_nodes, which is 4 in this case. To obtain the length of any individual array in the jagged array, you will use an expression such as this:

 network_nodes[0].Length

which, in this case, obtains the length of the first array.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

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