Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
swdev:dotnet:arrays [2011/04/08 11:18] smayr created |
swdev:dotnet:arrays [2011/05/24 15:48] (current) smayr [Resources] |
||
---|---|---|---|
Line 1: | Line 1: | ||
== Arrays == | == Arrays == | ||
- | |||
- | == Multi-Dimensional Arrays == | ||
=== Jagged Arrays === | === Jagged Arrays === | ||
Line 19: | Line 17: | ||
<code csharp> | <code csharp> | ||
// allocate memory | // allocate memory | ||
- | int array[][] = new int[2][10]; | + | int array[,] = new int[2][10]; |
// use array | // use array | ||
array[0,2] = 15; | array[0,2] = 15; | ||
</ | </ | ||
+ | |||
+ | === Getting Dimensions === | ||
+ | |||
+ | When traversing an array using loops, for example, there are a couple of ways to do it: | ||
+ | |||
+ | <code csharp> | ||
+ | // Single Dimension | ||
+ | int SingleDimArr[] = new int[10]; | ||
+ | for (int i = 0; i < SingleDimArr.Length; | ||
+ | { | ||
+ | SingleDimArr[i, | ||
+ | } | ||
+ | |||
+ | // Multi-dimensions | ||
+ | int DoubleDimArr[, | ||
+ | for (int i = 0; i < DoubleDimArr.GetLength(0); | ||
+ | { | ||
+ | for (int j = 0; j < DoubleDimArr.GetLength(1); | ||
+ | { | ||
+ | DoubleDimArr[i, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Alternatively, | ||
+ | <code csharp> | ||
+ | int[,] arr = new int[,] {{1,2,3}, {4,5,6}, {7,8,9}, {10, | ||
+ | foreach(int a[] in arr) | ||
+ | { | ||
+ | foreach(int i in a) | ||
+ | { | ||
+ | Console.WriteLine(i); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | == Resources == | ||
+ | * [[http:// | ||
+ | * [[http:// |