Multidimensional Arrays |
|
Beyond two dimensions, you can create a variable that represents various lists and each list contains various internal lists. This is referred to as a multidimensional array.
To create a multidimensional array, add as many commas in the square brackets as you judge them necessary. Here is an example of a three-dimensional array initialized: |
using System; namespace CSharpLessons { class Exercise { static int Main() { double[,,] number = new double[2,3,5] { { { 12.44, 525.38, -6.28, 2448.32, 632.04 }, {-378.05, 48.14, 634.18, 762.48, 83.02 }, { 64.92, -7.44, 86.74, -534.60, 386.73 } }, { { 48.02, 120.44, 38.62, 526.82, 1704.62 }, { 56.85, 105.48, 363.31, 172.62, 128.48 }, { 906.68, 47.12, -166.07, 4444.26, 408.62 } }, }; Console.WriteLine("Number of items {0}", number.Length); Console.WriteLine("Number of items {0}", number.Rank); Console.WriteLine(); return 0; } } } In this example, we are creating 2 groups of items. Each of the two groups is made of three lists. Each list contains 5 numbers. This would produce: Number of items 30 Number of items 3
To locate each member of a multidimensional array, type the name of the array followed by the opening square bracket, followed by the 0-based first dimension, followed by a comma. Continue with each dimension and end with the closing square bracket. Here is an example: using System; namespace CSharpLessons { class Exercise { static int Main() { double[,,] number = new double[2,3,5] { { { 12.44, 525.38, -6.28, 2448.32, 632.04 }, {-378.05, 48.14, 634.18, 762.48, 83.02 }, { 64.92, -7.44, 86.74, -534.60, 386.73 } }, { { 48.02, 120.44, 38.62, 526.82,1704.62 }, { 56.85, 105.48, 363.31, 172.62, 128.48 }, { 906.68, 47.12,-166.07, 4444.26, 408.62 } }, }; Console.WriteLine("Number[0][0][0] = {0}", number[0,0,0]); Console.WriteLine("Number[0][0][1] = {0}", number[0,0,1]); Console.WriteLine("Number[0][0][2] = {0}", number[0,0,2]); Console.WriteLine("Number[0][0][3] = {0}", number[0,0,3]); Console.WriteLine("Number[0][0][4] = {0}\n", number[0,0,4]); Console.WriteLine("Number[0][1][0] = {0}", number[0,1,0]); Console.WriteLine("Number[0][1][1] = {0}", number[0,1,1]); Console.WriteLine("Number[0][1][2] = {0}", number[0,1,2]); Console.WriteLine("Number[0][1][3] = {0}", number[0,1,3]); Console.WriteLine("Number[0][1][4] = {0}\n", number[0,1,4]); Console.WriteLine("Number[0][2][0] = {0}", number[0,2,0]); Console.WriteLine("Number[0][2][1] = {0}", number[0,2,1]); Console.WriteLine("Number[0][2][2] = {0}", number[0,2,2]); Console.WriteLine("Number[0][2][3] = {0}", number[0,2,3]); Console.WriteLine("Number[0][2][4] = {0}\n", number[0,2,4]); Console.WriteLine("Number[1][0][0] = {0}", number[1,0,0]); Console.WriteLine("Number[1][0][1] = {0}", number[1,0,1]); Console.WriteLine("Number[1][0][2] = {0}", number[1,0,2]); Console.WriteLine("Number[1][0][3] = {0}", number[1,0,3]); Console.WriteLine("Number[1][0][4] = {0}\n", number[1,0,4]); Console.WriteLine("Number[1][1][0] = {0}", number[1,1,0]); Console.WriteLine("Number[1][1][1] = {0}", number[1,1,1]); Console.WriteLine("Number[1][1][2] = {0}", number[1,1,2]); Console.WriteLine("Number[1][1][3] = {0}", number[1,1,3]); Console.WriteLine("Number[1][1][4] = {0}\n", number[1,1,4]); Console.WriteLine("Number[1][2][0] = {0}", number[1,2,0]); Console.WriteLine("Number[1][2][1] = {0}", number[1,2,1]); Console.WriteLine("Number[1][2][2] = {0}", number[1,2,2]); Console.WriteLine("Number[1][2][3] = {0}", number[1,2,3]); Console.WriteLine("Number[1][2][4] = {0}\n", number[1,2,4]); Console.WriteLine("Number of items {0}", number.Length); Console.WriteLine("Number of items {0}", number.Rank); Console.WriteLine(); return 0; } } } This would produce: Number[0][0][0] = 12.44 Number[0][0][1] = 525.38 Number[0][0][2] = -6.28 Number[0][0][3] = 2448.32 Number[0][0][4] = 632.04 Number[0][1][0] = -378.05 Number[0][1][1] = 48.14 Number[0][1][2] = 634.18 Number[0][1][3] = 762.48 Number[0][1][4] = 83.02 Number[0][2][0] = 64.92 Number[0][2][1] = -7.44 Number[0][2][2] = 86.74 Number[0][2][3] = -534.6 Number[0][2][4] = 386.73 Number[1][0][0] = 48.02 Number[1][0][1] = 120.44 Number[1][0][2] = 38.62 Number[1][0][3] = 526.82 Number[1][0][4] = 1704.62 Number[1][1][0] = 56.85 Number[1][1][1] = 105.48 Number[1][1][2] = 363.31 Number[1][1][3] = 172.62 Number[1][1][4] = 128.48 Number[1][2][0] = 906.68 Number[1][2][1] = 47.12 Number[1][2][2] = -166.07 Number[1][2][3] = 4444.26 Number[1][2][4] = 408.62 Number of items 30 Number of items 3
|
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|