The problem of determining the dimensional lattice of C # 3

There is a compilation error in my next code,

Error 1: It is not possible to implicitly convert the type 'TestArray1.Foo [, *]' to 'TestArray1.Foo [] [] []' C: \ Users \ lma \ Documents \ Visual Studio 2008 \ Projects \ TestArray1 \ TestArray1 \ Program.cs 17 30 TestArray1

Does anyone have any ideas? Here is my whole code, I am using VSTS 2008 + Vista 64-bit.

namespace TestArray1 { class Foo { } class Program { static void Main(string[] args) { Foo[][][] foos = new Foo[1, 1, 1]; return; } } } 

EDIT: version 2. I have a different version of the code, but still have a compilation error. Any ideas?

 Error 1 Invalid rank specifier: expected ',' or ']' C:\Users\lma\Documents\Visual Studio 2008\Projects\TestArray1\TestArray1\Program.cs 17 41 TestArray1 Error 2 Invalid rank specifier: expected ',' or ']' C:\Users\lma\Documents\Visual Studio 2008\Projects\TestArray1\TestArray1\Program.cs 17 44 TestArray1 namespace TestArray1 { class Foo { } class Program { static void Main(string[] args) { Foo[][][] foos = new Foo[1][1][1]; return; } } } 

EDIT: version 3. I think I want to have a gear array. And after studying from guys. Here is my code fix, and it compiles fine in VSTS 2008. What I want is a jagged array, and currently I only need to have one element. Can someone check if my code implements my goal correctly?

 namespace TestArray1 { class Foo { } class Program { static void Main(string[] args) { Foo[][][] foos = new Foo[1][][]; foos[0] = new Foo[1][]; foos[0][0] = new Foo[1]; foos[0][0][0] = new Foo(); return; } } } 

thanks in advance George

+4
source share
8 answers

As for version 2 of your code - unfortunately, you cannot specify jagged arrays in this way. Instead, you need to do:

 Foo[][][] foos = new Foo[1][][]; foos[0] = new Foo[1][]; foos[0][0] = new Foo[1]; 

You should fill each array separately, basically. Foo[][][] means "array of arrays of arrays of Foo". An initialization statement like this is only able to initialize one array at a time. With a rectangular array, you still get only one (multidimensional) array, so new Foo[1,1,1] valid.

If this is for real code, by the way, I urge you to at least consider other design solutions. Arrays of arrays can be useful, but you can easily run into such problems. Arrays of arrays of arrays are even more unpleasant. There may be more readable ways of expressing what interests you.

+7
source

Make up :)

You either want:

 Foo[,,] foos = new Foo[1, 1, 1]; 

or

 Foo[][][] foos = new Foo[1][1][1]; 
+3
source

You declare a double nested array (an array of an array of arrays) and assign a three-dimensional array. These two are completely different. You can change your ad to

 Foo[,,] foos = new Foo[1,1,1] 

if you want a truly three-dimensional array. Jagged arrays (type [][][] ) are not needed in C #, as, for example, in Java.

+2
source

The simplest answer is to use

 Foo[,,] foos = new Foo[2, 3, 4]; 

Which gives a 3-dimensional array as a contiguous block of memory 2 * 3 * 4 = 24 Foo.

An alternative looks like this:

 Foo[][][] foos = new Foo[2][][]; for (int a = 0; a < foos.Length; a++) { foos[a] = new Foo[3][]; for (int b = 0; b < foos[a].Length; b++) { foos[a][b] = new Foo [4]; for (int c = 0; c < foos[a][b].Length; c++) foos[a][b][c] = new Foo(); } } 

Although this jagged (= array of array) method works a little more, using it is faster. This is due to a lack of a compiler that will always check the range when accessing an element in the case of Foo [,], although it can at least optimize for-loops that use the Length property in Foo [] [] [].

Also see this question

+2
source

Depending on whether you want them to be serrated or not:

 //makes a 5 by 4 by 3 array: string[,,] foos = new string[5,4,3]; 

http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

Oh and here the values ​​are initialized:

  char[, ,] blah = new char[2, 2, 2] { {{ '1', '2' }, { '3', '4' }}, {{ '5', '6' }, { '7', '8' }} }; 

Please note that this will not work:

 Foo[][][] foos = new Foo[1][1][1]; 

Since you are using jagged array syntax that does not allow you to determine the size of nested arrays. Use this instead:

 Foo[,,] foos = new Foo[1][1][1]; //1 by 1 by 1 

or

 Foo[][][] foos = new Foo[1][][]; //1 array allowing two nested levels of jagged arrays foos[0] = new Foo[1]; //for the first element, create a new array nested in it foos[0][0] = new Foo[1]; //create a third level new array nested in it 
+1
source

These two different array declarations create very different things.

Consider a simpler case:

 Foo[,] twoDimensionArray = new Foo[5,5]; 

This array has two dimensions - you can think of it as a table. You need both axes to return something:

 Foo item = twoDimensionArray[2,3] 

Indexes always have the same length - in this case, 0-4.

The jammed array is actually an array of arrays:

 Foo[][] jaggedArray = new Foo[5][]; jaggedArray[0] = new Foo[2]; jaggedArray[1] = new Foo[4]; ... 

If you use only one axis index, it will return an array:

 Foo[] oneRow = jaggedArray[3]; 

If you use both options, you make your choice from the submatrix:

 Foo item = jaggedArray[3][2]; //would be the same as: Foo item = oneRow[2]; 

Each of these subarrays may have different lengths or may not even be filled.

+1
source

If this means something, just for a C # declaration:

 int[,,] ary = new int[,,] { { { 111, 112 }, { 121, 122 }, { 131, 132 } } , { { 211, 212 }, { 221, 222 }, { 231, 232 } } , { { 311, 312 }, { 321, 322 }, { 331, 332 } } , { { 411, 412 }, { 421, 422 }, { 431, 432 } } }; 
+1
source

Array initialization

 int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } } 

source: Multidimensional Arrays (C # Programming Guide)

0
source

All Articles