我有一个如下所示的锯齿状数组,数组在c#代码中:
@{
int[][] array = new int[2][4];
// codes here that manipulates the values of the array.
}
现在我想获取/遍历数组中的值。但是下面的代码不起作用。当我运行程序时,我得到一个运行时错误“索引超出了数组的范围。”
for(var i = 0 ; i < @array.Count(); i++){
alert( '@array['i'].Length');
}
怎么做?
由于
答案 0 :(得分:1)
尝试类似
的内容foreach(var subArray in array)
{
@:alert(subArray.Length);
}
但由于它是静态声明的长度总是不一样吗?
答案 1 :(得分:1)
遍历多维数组:
int[,] a = new int[,]
{
{2, 4}
};
for (int i = 0; i <= a.GetUpperBound(0); i++)
{
for (int k = 0; k <= a.GetUpperBound(1); k++)
{
// a[i, k];
}
}
答案 2 :(得分:0)
我能看到的唯一问题是剃刀本身有点......奇怪;目前尚不清楚alert
是打算是cshtml还是输出,但该行看起来不合适。
如果输出alert
,您可以尝试:
// note this might need to be @for, depending on the line immediately before
for(var i = 0 ; i < array.Length; i++) {
@:alert('@(array[i].Length)');
}
如果alert
用作服务器端:
// note this might need to be @for, depending on the line immediately before
for(var i = 0 ; i < array.Length; i++) {
alert(array[i].Length);
}