c#也提供了双折叠数组约束


int[] x = { 1, 2, 3, 4,5 };
int[] y = { 5, 4, 3, 2, 1 };
int[,] s=new int[(x.Length)*(x.Length),2];

for (int i = 0; i < x.Length; i++)
{
for (int j = 0; j < y.Length; i++)
{
s[i, j] = x[i] * y[j];
Console.WriteLine(x[i] + " * " + y[j] + " = " + s[i, j]);
}
}

IndexOutOfRangeException: Index was outside the bounds of array.

它给出了一个内存错误,但它说它挂起了变量的内存,但我传输的不仅仅是序列的字段。我不知道问题出在哪里。你能帮忙吗?

IndexOutOfRangeException: Index超出了数组的边界。

改变:

for (int i = 0; i < x.Length; i++)
{
for (int j = 0; j < y.Length; i++)

:

for (int i = 0; i <= x.Length-1; i++)
{
for (int j = 0; j <= y.Length-1; j++)

还有这一行:

int[,] s=new int[(x.Length)*(x.Length),2];

虽然没有大的问题,但如果x[]和y[]的长度不同,可能会导致存储i,j值的问题。你的意思是像这样声明吗? =

int[,] s=new int[(x.Length)*(y.Length),2];

相关内容

  • 没有找到相关文章