2012-07-19 36 views
-2

我附上下面的代碼,我得到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; 
      } 
     } 
+0

哪行引發異常?這條線上陣列的形狀是什麼?簡單的答案是,在代碼中使用的數組索引之一不存在於數組中。 – David 2012-07-19 15:57:39

+0

我提到過,它是A部分,數組是3D數組 – Rahul 2012-07-19 15:58:28

+0

拋出異常時'array'值的形狀是什麼?和'j'的價值?數組的第一維沒有索引2或1,或者第二維沒有索引「j」。 – David 2012-07-19 16:00:12

回答

0

你有陣列12次不同的訪問,可能是這個問題,你不讓我們看看數組是如何創建的。任何理智的人可以幫助您與您的特定問題,

所以不是我會試着回答這個問題

如何調試IndexOutOfRangeException

  • 在Visual Studio中打開例外窗口
  • 展開公共語言運行庫異常窗口
  • 展開系統節點
  • 檢查時拋出複選框System.IndexOutOfRangeException

當你的調試器會發生異常。現在通過監視,工具提示,命令或直接窗口檢查變量。我相信你會很快找到問題所在。

enter image description here

注意:您可以通過

  • 菜單項訪問例外窗口調試 - >例外
  • 鍵盤快捷鍵按Ctrl + Alt + E
  • 鍵盤快捷鍵按Ctrl + D,E
  • 命令窗口通過>Debug.Exceptions
0

難道字符串S1,S2,是絕對位置,你實際上應該做的:

int j = Int32.Parse(s1) -1; 

int k = Int32.Parse(s2) -1;