【发布时间】:2017-09-12 13:22:05
【问题描述】:
好的,我正在尝试使用 Length 属性搜索二维数组(我必须使用它,否则我只使用 GetLength() )。该数组随机填充一组行数和列数。它会询问用户要搜索的数字,然后它将搜索数组并返回真或假,如果找到该数字,则返回行和列的索引。
我对我为正确设置 for 循环所做的研究中的代码相当有信心,但目前我收到一条错误消息,提示“[] 内的索引数量错误;预期为 2”尝试搜索数组的列。
我查看了这个错误,发现这应该是正确的设置。所以,我不确定这个循环的问题出在哪里,有人可以看看让我知道我错过了什么步骤吗?
谢谢!
int [,] math;
math = new int[3, 5]; //So you can see my array that is declared in main
static bool SearchArray(int search, int [,] array, out int row, out int coln)
{
row = -1;
coln = -1;
// search parameter is given from another method where it asks the user for a number to search for in the array.
for (int x = 0; x < array.Length; x++)
{
for (int y = 0; y < array[x].Length; y++) //error is here with the array[x] and continues to any reference I try to make similarly.
{
if (array[x][y] == search)
{
row = array[x];
coln = array[y];
return true;
}
}
}
return false
【问题讨论】:
标签: c# arrays loops search multidimensional-array