我附上下面的代碼,我得到IndexOutOfRangeException A部分沒有被處理,我試過的try-catch如下,但現在我得到類型的IndexOutOfRangeException多維數組
「第一次機會異常「 System.IndexOutOfRangeException'發生在app.Form6.ZMove(String s1,String s2)中的app.exe 中C:\ Users \ Rahul Taneja \ Documents \ Visual Studio 2010 \ Projects \ app \ app \ Form6.cs中:第136行「
在堆棧跟蹤任何一個可以告訴我爲什麼t正在發生,可能會有什麼解決方案?
public void ZMove(string s1, string s2)
{
//Move 2-1-4-3
int j = Int32.Parse(s1);
int k = Int32.Parse(s2);
for (int l = 0; l < k; l++)
{
try
{
swap(array[2][j], array[1][j]); ///Part A
swap(array[1][j], array[4][j]);
swap(array[4][j], array[3][j]);
swap(array[3][j], array[2][j]);
}
catch (IndexOutOfRangeException e)
{
MessageBox.Show(e.StackTrace);
//throw;
}
}
}
private void swap(char[] p1, char[] p2)
{
//throw new NotImplementedException();
int l = p1.Length;
for (int i = 0; i < l; i++)
{
char temp = p1[i];
p1[i] = p2[i];
p2[i] = temp;
}
}
哪行引發異常?這條線上陣列的形狀是什麼?簡單的答案是,在代碼中使用的數組索引之一不存在於數組中。 – David 2012-07-19 15:57:39
我提到過,它是A部分,數組是3D數組 – Rahul 2012-07-19 15:58:28
拋出異常時'array'值的形狀是什麼?和'j'的價值?數組的第一維沒有索引2或1,或者第二維沒有索引「j」。 – David 2012-07-19 16:00:12