C# Don';我不知道IndexOutOfRangeException是什么意思

C# Don';我不知道IndexOutOfRangeException是什么意思,c#,arrays,indexoutofrangeexception,C#,Arrays,Indexoutofrangeexception,我正在尝试c#中的插入排序算法,并努力修复此错误消息: “algorithmsAssignment.exe中出现System.IndexOutoforAngeException” 一旦它到达while循环,代码就会中断并给出消息。任何帮助都将不胜感激 (我必须执行string.compare,因为我使用的是2D数组字符串 static void insertionSort(int columnSort, bool accendingOrder) { int column

我正在尝试c#中的插入排序算法,并努力修复此错误消息:

“algorithmsAssignment.exe中出现System.IndexOutoforAngeException”

一旦它到达while循环,代码就会中断并给出消息。任何帮助都将不胜感激

(我必须执行string.compare,因为我使用的是2D数组字符串

static void insertionSort(int columnSort, bool accendingOrder)
    {
        int column = columnSort - 1;
        int i, j;

        for (i = 1; i < dataArray.GetLength(1); i++)
        {
            string key = dataArray[column, i];
            j = i - 1;

            /* Move elements of arr[0..i-1], that are
               greater than key, to one position ahead
               of their current position */
            while (j >= 0 && string.Compare(dataArray[column, j - 1], 
dataArray[j, column]) > 0)
            {
                dataArray[column, j + 1] = dataArray[column, j];
                j = j - 1;
            }
            dataArray[column, j + 1] = key;
        }
    }
静态void insertionSort(int-columnSort,bool-ackendingorder)
{
int column=columnSort-1;
int i,j;
对于(i=1;i=0&&string.Compare(dataArray[column,j-1],
数据数组[j,列]>0)
{
dataArray[column,j+1]=dataArray[column,j];
j=j-1;
}
dataArray[列,j+1]=键;
}
}

在第一次迭代中:(i=1)

我打赌你的索引超出了范围,因为索引-1不可能存在


干杯

i=1时,您将得到错误,因为您有以下条件:

 j = i - 1; //j=0 for i=1
while循环中的错误条件

while (j >= 0 && string.Compare(dataArray[column, j - 1], 
dataArray[j, column]) > 0)
while循环
dataArray[column,j-1]
中的此条件将引发
indexoutfrange
异常,因为

j-1=-1 for j=0

我不想吓唬你,但我认为你使用的索引很有可能小于零,或大于或等于索引数组中的项目数。你有没有尝试在文档中查找它?描述得很好
j-1=-1 for j=0