My following code has compile error,
我的以下代码有编译错误,
Error 1 Cannot implicitly convert type 'TestArray1.Foo[,,*]' to 'TestArray1.Foo[][][]' C:\Users\lma\Documents\Visual Studio 2008\Projects\TestArray1\TestArray1\Program.cs 17 30 TestArray1
错误1无法将类型'TestArray1.Foo [,, *]'隐式转换为'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.
有没有人有任何想法?这是我的整个代码,我使用的是VSTS 2008 + Vista 64位。
namespace TestArray1
{
class Foo
{
}
class Program
{
static void Main(string[] args)
{
Foo[][][] foos = new Foo[1, 1, 1];
return;
}
}
}
EDIT: version 2. I have another version of code, but still has compile error. Any ideas?
编辑:版本2.我有另一个版本的代码,但仍然有编译错误。有任何想法吗?
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 jagged array. And after learning from the fellow guys. Here is my code fix, and it compile fine in VSTS 2008. What I want is a jagged array, and currently I need to have only one element. Could anyone review whether my code is correct to implement my goal please?
编辑:版本3.我想我想要一个锯齿状的数组。在向同伴们学习之后。这是我的代码修复,它在VSTS 2008中编译良好。我想要的是锯齿状数组,目前我只需要一个元素。有人可以检查我的代码是否正确实现我的目标吗?
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
乔治,提前谢谢
8 个解决方案
#1
Regarding version 2 of your code - unfortunately you can't specify jagged arrays in that way. Instead, you need to do:
关于代码的第2版 - 遗憾的是,您不能以这种方式指定锯齿状数组。相反,你需要做:
Foo[][][] foos = new Foo[1][][];
foos[0] = new Foo[1][];
foos[0][0] = new Foo[1];
You have to populate each array separately, basically. Foo[][][]
means "an array of arrays of arrays of Foo." An initialization statement like this is only capable of initializing one array at a time. With the rectangular array, you still end up with just a single (multi-dimensional) array, which is why new Foo[1,1,1]
is valid.
基本上,你必须分别填充每个数组。 Foo [] [] []表示“Foo数组数组的数组”。像这样的初始化语句只能一次初始化一个数组。使用矩形数组,您最终只得到一个(多维)数组,这就是新Foo [1,1,1]有效的原因。
If this is for real code by the way, I'd urge you to at least consider other design decisions. Arrays of arrays can be useful, but you can easily run into problems like this. Arrays of arrays of arrays are even nastier. There may be more readable ways of expressing what you're interested in.
如果这是真正的代码,我建议你至少考虑其他设计决策。数组数组可能很有用,但您很容易遇到这样的问题。阵列数组的阵列甚至更糟糕。可能有更可读的方式来表达您感兴趣的内容。
#2
Make up your mind :)
下定决心:)
You either want:
你要么:
Foo[,,] foos = new Foo[1, 1, 1];
or:
Foo[][][] foos = new Foo[1][1][1];
#3
You are declaring a double-nested array (array of array of array) and assigning a three-dimensional array. Those two are definitely different. You can change your declaration to
您正在声明一个双嵌套数组(数组数组的数组)并分配一个三维数组。这两个肯定是不同的。您可以将声明更改为
Foo[,,] foos = new Foo[1,1,1]
if you want a truly three-dimensional array. Jagged arrays (the [][][]
kind) are not that needed in C# as in, say, Java.
如果你想要一个真正的三维数组。 Jagged数组([] [] []种类)不是C#中需要的,比如Java。
#4
The simplest answer is to use
最简单的答案是使用
Foo[,,] foos = new Foo[2, 3, 4];
Which gives you a 3-dimensional array as a contiguous block of memory of 2*3*4=24 Foo's.
这给你一个三维数组作为2 * 3 * 4 = 24 Foo的连续内存块。
The alternative looks like :
替代方案如下:
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) approach is a bit more work, using it is actually faster. This is caused by a shortcoming of the compiler that will always do a range check when accessing an element in the Foo[,,] case, while it is able to at least optimize for-loops that use the Length property in the Foo[][][] scenario.
虽然这种锯齿状(=数组数组)方法有点多功,但使用它实际上更快。这是由于编译器的缺点导致在访问Foo [,,]情况下的元素时总是进行范围检查,而它能够至少优化使用Foo []中的Length属性的for循环。 [] []场景。
Also see this question
另见这个问题
#5
These two different array declarations create very different things.
这两个不同的数组声明创建了非常不同的东西。
Let's look at simpler case:
让我们看一下更简单的案例:
Foo[,] twoDimensionArray = new Foo[5,5];
This array has two dimensions - you can think of it as a table. You need both axis in order to return anything:
此数组有两个维度 - 您可以将其视为一个表。你需要两个轴才能返回任何东西:
Foo item = twoDimensionArray[2,3]
The indexes always have the same lengths - in this case 0-4.
索引总是具有相同的长度 - 在这种情况下为0-4。
A jagged array is actually an array of arrays:
锯齿状数组实际上是一个数组数组:
Foo[][] jaggedArray = new Foo[5][];
jaggedArray[0] = new Foo[2];
jaggedArray[1] = new Foo[4];
...
If you only use one axis index it will return an array:
如果您只使用一个轴索引,它将返回一个数组:
Foo[] oneRow = jaggedArray[3];
If you use both you make your selection from the sub-array:
如果您同时使用这两个,则从子数组中进行选择:
Foo item = jaggedArray[3][2];
//would be the same as:
Foo item = oneRow[2];
Each of these sub-arrays can have a different length, or even not be populated.
这些子阵列中的每一个可以具有不同的长度,或甚至不被填充。
#6
Depends on whether you want them to be jagged 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 is initializing values:
哦,这里是初始化值:
char[, ,] blah = new char[2, 2, 2] {
{{ '1', '2' }, { '3', '4' }},
{{ '5', '6' }, { '7', '8' }}
};
Note that this will not work:
请注意,这不起作用:
Foo[][][] foos = new Foo[1][1][1];
Because you are using the jagged array syntax, which does not let you define the size of nested arrays. Instead use do this:
因为您使用的是锯齿状数组语法,它不允许您定义嵌套数组的大小。而是使用这样做:
Foo[,,] foos = new Foo[1][1][1]; //1 by 1 by 1
or this
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
#7
If it means anything, for just the C# declaration:
如果它意味着什么,只为C#声明:
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 } }
};
#8
Array Initialization
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } }
source: Multidimensional Arrays (C# Programming Guide)
source:多维数组(C#编程指南)
#1
Regarding version 2 of your code - unfortunately you can't specify jagged arrays in that way. Instead, you need to do:
关于代码的第2版 - 遗憾的是,您不能以这种方式指定锯齿状数组。相反,你需要做:
Foo[][][] foos = new Foo[1][][];
foos[0] = new Foo[1][];
foos[0][0] = new Foo[1];
You have to populate each array separately, basically. Foo[][][]
means "an array of arrays of arrays of Foo." An initialization statement like this is only capable of initializing one array at a time. With the rectangular array, you still end up with just a single (multi-dimensional) array, which is why new Foo[1,1,1]
is valid.
基本上,你必须分别填充每个数组。 Foo [] [] []表示“Foo数组数组的数组”。像这样的初始化语句只能一次初始化一个数组。使用矩形数组,您最终只得到一个(多维)数组,这就是新Foo [1,1,1]有效的原因。
If this is for real code by the way, I'd urge you to at least consider other design decisions. Arrays of arrays can be useful, but you can easily run into problems like this. Arrays of arrays of arrays are even nastier. There may be more readable ways of expressing what you're interested in.
如果这是真正的代码,我建议你至少考虑其他设计决策。数组数组可能很有用,但您很容易遇到这样的问题。阵列数组的阵列甚至更糟糕。可能有更可读的方式来表达您感兴趣的内容。
#2
Make up your mind :)
下定决心:)
You either want:
你要么:
Foo[,,] foos = new Foo[1, 1, 1];
or:
Foo[][][] foos = new Foo[1][1][1];
#3
You are declaring a double-nested array (array of array of array) and assigning a three-dimensional array. Those two are definitely different. You can change your declaration to
您正在声明一个双嵌套数组(数组数组的数组)并分配一个三维数组。这两个肯定是不同的。您可以将声明更改为
Foo[,,] foos = new Foo[1,1,1]
if you want a truly three-dimensional array. Jagged arrays (the [][][]
kind) are not that needed in C# as in, say, Java.
如果你想要一个真正的三维数组。 Jagged数组([] [] []种类)不是C#中需要的,比如Java。
#4
The simplest answer is to use
最简单的答案是使用
Foo[,,] foos = new Foo[2, 3, 4];
Which gives you a 3-dimensional array as a contiguous block of memory of 2*3*4=24 Foo's.
这给你一个三维数组作为2 * 3 * 4 = 24 Foo的连续内存块。
The alternative looks like :
替代方案如下:
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) approach is a bit more work, using it is actually faster. This is caused by a shortcoming of the compiler that will always do a range check when accessing an element in the Foo[,,] case, while it is able to at least optimize for-loops that use the Length property in the Foo[][][] scenario.
虽然这种锯齿状(=数组数组)方法有点多功,但使用它实际上更快。这是由于编译器的缺点导致在访问Foo [,,]情况下的元素时总是进行范围检查,而它能够至少优化使用Foo []中的Length属性的for循环。 [] []场景。
Also see this question
另见这个问题
#5
These two different array declarations create very different things.
这两个不同的数组声明创建了非常不同的东西。
Let's look at simpler case:
让我们看一下更简单的案例:
Foo[,] twoDimensionArray = new Foo[5,5];
This array has two dimensions - you can think of it as a table. You need both axis in order to return anything:
此数组有两个维度 - 您可以将其视为一个表。你需要两个轴才能返回任何东西:
Foo item = twoDimensionArray[2,3]
The indexes always have the same lengths - in this case 0-4.
索引总是具有相同的长度 - 在这种情况下为0-4。
A jagged array is actually an array of arrays:
锯齿状数组实际上是一个数组数组:
Foo[][] jaggedArray = new Foo[5][];
jaggedArray[0] = new Foo[2];
jaggedArray[1] = new Foo[4];
...
If you only use one axis index it will return an array:
如果您只使用一个轴索引,它将返回一个数组:
Foo[] oneRow = jaggedArray[3];
If you use both you make your selection from the sub-array:
如果您同时使用这两个,则从子数组中进行选择:
Foo item = jaggedArray[3][2];
//would be the same as:
Foo item = oneRow[2];
Each of these sub-arrays can have a different length, or even not be populated.
这些子阵列中的每一个可以具有不同的长度,或甚至不被填充。
#6
Depends on whether you want them to be jagged 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 is initializing values:
哦,这里是初始化值:
char[, ,] blah = new char[2, 2, 2] {
{{ '1', '2' }, { '3', '4' }},
{{ '5', '6' }, { '7', '8' }}
};
Note that this will not work:
请注意,这不起作用:
Foo[][][] foos = new Foo[1][1][1];
Because you are using the jagged array syntax, which does not let you define the size of nested arrays. Instead use do this:
因为您使用的是锯齿状数组语法,它不允许您定义嵌套数组的大小。而是使用这样做:
Foo[,,] foos = new Foo[1][1][1]; //1 by 1 by 1
or this
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
#7
If it means anything, for just the C# declaration:
如果它意味着什么,只为C#声明:
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 } }
};
#8
Array Initialization
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } }
source: Multidimensional Arrays (C# Programming Guide)
source:多维数组(C#编程指南)