Sponsored Ad

Friday, November 27, 2009

C# 2D Array

Trouble: You want to use a 2D array containing any type of value in your program in C #. Iterate through each element with a loop, find the proper bounds of the array. Solution. Two-dimensional arrays are more difficult to treat than the one-dimensional arrays. Here is some advice on how to use 2D arrays in your programs using the C # programming language.

2D Array Benchmark

Looping with GetUpperBound: 142 ms

Looping with Length/2: 47 ms

1. Using two-dimensional arrays

First of all here we can see some sample uses two-dimensional arrays in C #. The party then initializes a 2D matrix, then B, C and D, all iterate over it. The sample does not change the values of the matrix. Displays the syntax of indexing.

+++ Program that uses two-dimensional arrays (C#) +++

Code Snippet
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         // A. 2D array of strings.
  8.         string[,] a = new string[,]
  9.         {
  10.             {"ant", "aunt"},
  11.             {"Sam", "Samantha"},
  12.             {"clozapine", "quetiapine"},
  13.             {"flomax", "volmax"},
  14.             {"toradol", "tramadol"}
  15.         };

 

Code Snippet
  1. // B. Get the upper bound to loop.
  2.         for (int i = 0; i <= a.GetUpperBound(0); i++)
  3.         {
  4.             string s1 = a[i, 0]; // ant, Sam, clozapine...
  5.             string s2 = a[i, 1]; // aunt, Samantha, quetiapine...
  6.             Console.WriteLine("{0}, {1}", s1, s2);
  7.         }
  8.         Console.WriteLine();
  9.  
  10.         // C. Loop based on length.
  11.         // ... Assumes each subarray is two elements long.
  12.         for (int i = 0; i < a.Length / 2; i++)
  13.         {
  14.             string s1 = a[i, 0];
  15.             string s2 = a[i, 1];
  16.             Console.WriteLine("{0}, {1}", s1, s2);
  17.         }
  18.         Console.WriteLine();
  19.  
  20.         // D. Get both bounds.
  21.         int bound0 = a.GetUpperBound(0);
  22.         int bound1 = a.GetUpperBound(1);
  23.         for (int i = 0; i <= bound0; i++)
  24.         {
  25.             for (int x = 0; x <= bound1; x++)
  26.             {
  27.                 string s1 = a[i, x];
  28.                 Console.WriteLine(s1);
  29.             }
  30.             Console.WriteLine();
  31.         }
  32.     }
  33. }
  34.  
  35. +++ Output of the program +++
  36. ant, aunt
  37. Sam, Samantha
  38. clozapine, quetiapine
  39. flomax, volmax
  40. toradol, tramadol
  41.  
  42. ant, aunt
  43. Sam, Samantha
  44. clozapine, quetiapine
  45. flomax, volmax
  46. toradol, tramadol
  47. ant
  48. aunt
  49. Sam
  50. Samantha
  51. clozapine
  52. quetiapine
  53. flomax
  54. volmax
  55. toradol
  56. tramadol

1A. Example 2D array declared. This text describes the behavior of part A in the example code. We see how to declare 2D arrays. Note the syntax confusing, with braces and commas. Just memorize the syntax. This is what we see in the Visual Studio.

clip_image002

Description of the Visual Studio. What we see is that the compiler sees the string [,] array as a string [5, 2] matrix. It follows automatically resized. This means that it is necessary to specify the length of the array always at compile time.

1B. Use GetUpperBound. This paper describes the use of GetUpperBound in the sample code. Here GetUpperBound method is used in the matrix. Note however that this method is probably not the best basis for a simple 2D.

Code Snippet
  1. for (int i = 0; i <= a.GetUpperBound(0); i++)
  2. {
  3.     string s1 = a[i, 0];
  4.     string s2 = a[i, 1];
  5.     Console.WriteLine("{0}, {1}", s1, s2);
  6. }

1C. 2D array to iterate over Largo. This part of the code shows how you can use the loop length array more efficient. The quickest method for a wide 2D is to do a little arithmetic. In this example, there are 5 rows. GetUpperBound (0) return 4. Taking long, which is 10, and divide by 2, we get 5.

Code Snippet
  1.   for (int i = 0; i < a.Length / 2; i++)
  2. {
  3.     string s1 = a[i, 0];
  4.     string s2 = a[i, 1];
  5.     Console.WriteLine("{0}, {1}", s1, s2);
  6. }

1D. Ensure that both the boundaries and keep them. This text describes how bounds of the array can be cached in local variables for better performance and clarity. Part D above shows how you can get the two dimensions of the array at runtime, and then iterate through the ranks. This example also shows how to cache limits.

Code Snippet
  1. int bound0 = a.GetUpperBound(0);
  2. int bound1 = a.GetUpperBound(1);
  3. for (int i = 0; i <= bound0; i++)
  4. {
  5.     for (int x = 0; x <= bound1; x++)
  6.     {
  7.         string s1 = a[i, x];
  8.         Console.WriteLine(s1);
  9.     }
  10.     Console.WriteLine();
  11. }

1. How slow is GetUpperBound

It's slow and does not want to call frequently. It took me a benchmark comparison 1 million repetitions of B and C, with the sample matrix. This illustrates the decrease in performance with GetUpperBound. You can see the reference point at the top of this document.

2. Parameters

Here we note that you can specify that a method receives a parameter of type two dimensional matrix. Use the type with the syntax of coma. The 2D array will be passed as a reference which means that changes will affect you for the original version as well. You can find more detailed information about 2D arrays as parameters to methods on this site.

0 comments:

Post a Comment

Sponsored Ad

More Related Articles

Website Update

Followers