What is wrong with my multidimensional array declaration? FROM#

private int[, ,] table = new int[4 , 5 , 5]{ {{0,6,2,6,4},{2,2,4,2,8},{4,4,8,4,6},{6,6,2,6,4},{8,8,6,8,2}}, {{0,2,8,8,4},{2,4,6,6,8},{4,8,2,2,6},{6,2,8,8,4},{8,6,4,4,2}}, {{0,4,2,4,4},{2,8,4,8,8},{4,6,8,6,6},{6,4,2,4,4},{8,2,6,2,2}}, {{0,8,8,2,4},{2,6,6,4,8},{4,2,2,8,6},{6,8,8,2,4},{8,4,4,6,2}} }; 

I want this table:

 k|l 0 1 2 3 4 0 06264 22428 44846 66264 88682 1 02884 24668 48226 62884 86442 2 04244 28488 46866 64244 82622 3 08824 26648 42286 68824 84462 

thanks for the help

+4
source share
7 answers

There is nothing wrong with your declaration. It compiles fine, and I can write something like this:

  for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { for (int k = 0; k < 5; k++) { var x = table[i, j, k]; } } } 

Update: I am sure you can cut it a bit. Since the dimensions of the array are determined by your initial values, you can write this as follows:

  private int[,,] table = new [,,]{ <your numbers go here> }; 
+2
source

you need a 2-dimensional array, its array [4,5]

0
source

Try to skip the new int[4 , 5 , 5] part new int[4 , 5 , 5] , the compiler should be able to determine the sizes. Otherwise, I do not see any problems.

0
source

The compiler can determine the length of each dimension for you, so let it do all the hard work:

 private int[,,] table = new int[,,]{ {{0,6,2,6,4},{2,2,4,2,8},{4,4,8,4,6},{6,6,2,6,4},{8,8,6,8,2}}, {{0,2,8,8,4},{2,4,6,6,8},{4,8,2,2,6},{6,2,8,8,4},{8,6,4,4,2}}, {{0,4,2,4,4},{2,8,4,8,8},{4,6,8,6,6},{6,4,2,4,4},{8,2,6,2,2}}, {{0,8,8,2,4},{2,6,6,4,8},{4,2,2,8,6},{6,8,8,2,4},{8,4,4,6,2}} }; 

And this gives you the desired result, except for the title:

  StringBuilder stringBuilder = new StringBuilder(); for (int row = 0; row < table.GetLength(0); ++row) { stringBuilder.Append(row.ToString() + "\t"); for (int column = 0; column < table.GetLength(1); ++column) { for (int valueIndex = 0; valueIndex < table.GetLength(2); ++valueIndex) { stringBuilder.Append(table[row, column, valueIndex].ToString()); } stringBuilder.Append("\t"); } stringBuilder.AppendLine(); } string result = stringBuilder.ToString(); Console.WriteLine(result); 
0
source

Just change new int[4 , 5 , 5] to new[,,]

This is what you need:

 var table = new[,,] { {{0, 6, 2, 6, 4}, {2, 2, 4, 2, 8}, {4, 4, 8, 4, 6}, {6, 6, 2, 6, 4}, {8, 8, 6, 8, 2}}, {{0, 2, 8, 8, 4}, {2, 4, 6, 6, 8}, {4, 8, 2, 2, 6}, {6, 2, 8, 8, 4}, {8, 6, 4, 4, 2}}, {{0, 4, 2, 4, 4}, {2, 8, 4, 8, 8}, {4, 6, 8, 6, 6}, {6, 4, 2, 4, 4}, {8, 2, 6, 2, 2}}, {{0, 8, 8, 2, 4}, {2, 6, 6, 4, 8}, {4, 2, 2, 8, 6}, {6, 8, 8, 2, 4}, {8, 4, 4, 6, 2}} }; 
0
source

There is nothing bad. It works flawlessly for me. What is your problem?

0
source

This piece of code will not compile if you have it in the wrong place - if you have a function inside, discard the private modifier. (If it is declared at the class level, then this is normal).

0
source

Source: https://habr.com/ru/post/1299816/


All Articles