2012-07-19 66 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;