好吧,我試圖使用length屬性來搜索一個二維數組(我不得不使用這個本來我只是用對GetLength())。該數組隨機填充一定數量的行和列。它將要求用戶輸入一個數字進行搜索,然後搜索該數組並返回true或false,並在發現該數字時發回行和列的索引。搜索一個使用二維數組長度屬性
我從研究,我已經做了讓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
只是爲了澄清,我必須使用對GetLength()來搜索,有沒有其他的選項可以使用,而不是對GetLength()? – ZLackLuster
是的。 GetLength方法獲取選定排名的長度。 Length屬性只返回數組中的總項目大小。這是最精確的方法。 – DigheadsFerke