[英]How to assign a value to an index of a three dimensional array?
I have defined two three dimensional arrays as: 我将两个三维数组定义为:
int[, ,] Link_k = new int[length1, length1, length1];
int[][][] Link_k2 = new int[length1][][];
where length1
is variable and could be any integer number. 其中length1
是可变的,可以是任何整数。
My question is that how could I assign a value for a special index or for all first indices. 我的问题是如何为一个特殊索引或所有第一个索引分配一个值。 I tried 我试过了
Link_k.SetValue(-1,(0,,));
Link_k2.SetValue(-1,[0][][]);
But that does not compile. 但这不能编译。
If you want to set the first index of every Z-axis array, you have to iterate over it, using for
for example. 如果你想设置每一个Z轴数组的第一个索引,你必须遍历它,使用for
例如。
For Link_k
: 对于Link_k
:
for (int x = 0; x < Array.GetUpperBound(Link_k, 0); x++)
{
for (int y = 0; i < Array.GetUpperBound(Link_k, 1); y++)
{
Link_k[x, y, 0] = -1;
}
}
And for Link_k2
: 对于Link_k2
:
int[][][] Link_k2 = new int[length1][][];
for (int x = 0; x < Link_k2.Length; x++)
{
Link_k2[x] = new int[length1][];
for (int y = 0; i < Link_k2[x].Length; y++)
{
Link_k2[x][y] = new int[length1];
Link_k2[x][y][0] = -1;
}
}
(Note you don't seem to assign the second and third array. Assign that in a for
loop, so you assign every array in every array, etc. so I have put that in too) (请注意,您似乎并未分配第二个和第三个数组。在for
循环中分配它,因此您在每个数组中分配了每个数组,依此类推,所以我也将其放入了)
As @Patrick Hofman said, Link_k is pretty easy: 正如@Patrick Hofman所说,Link_k非常简单:
Link_k[x, y, 0] = -1;
Or, using SetValue
: 或者,使用SetValue
:
Link_k.SetValue( -1, x, y, 0 );
However, you don't actually create a three dimensional array for Link_k2
- you create a one dimensional array of arrays of arrays. 但是,您实际上并没有为Link_k2
创建三维数组-您创建的是数组的一维数组。 Eg Link_k2[0]
is int[][]
, and, when it is initialised, Link_k2[0][0]
is int[]
. 例如, Link_k2[0]
为int[][]
,初始化时, Link_k2[0][0]
为int[]
。
So, for Link_k2
you'd need to: 因此,对于Link_k2
您需要:
for (int x = 0; x < Link_k2.Length; x++)
{
//create a new array of arrays at Link_k2[x]
Link_k2[x] = new int[length1][];
for (int y = 0; y < Link_k2[x].Length; y++)
{
//create a new arrays at Link_k2[x][y]
Link_k2[x][y] = new int[length1];
for (int z = 0; z < Link_k2[x][y].Length; z++)
{
Link_k2[x][y][z] = -1;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.