系统。IndexOutOfRangeException - Index不在数组的范围内[duplicate]

时间:2022-01-21 16:39:19

This question already has an answer here:

这个问题已经有了答案:

I am creating a program that imports a 2d object array of information from an excel sheet. It then passes this array to ProcessObjects method to be processed and printed/exported back out to an excel template.Can anyone tell me why I'm getting this error message?

我正在创建一个程序,它从一个excel表导入一个2d对象数组。然后,它将这个数组传递给ProcessObjects方法,并将其打印/导出到excel模板。有人能告诉我为什么我收到这个错误信息吗?

"An unhandled exception of type 'System.IndexOutOfRangeException' occurred in Project.exe

类型系统的未处理异常。IndexOutOfRangeException”发生在当

Additional information: Index was outside the bounds of the array."

附加信息:索引不在数组的范围之内。

private void ProcessObjects(object[,] classesArray, object[,] classesAvailabilityArray, Excel.Workbook workbook2, Excel.Sheets excelSheets)
{
    // once classes are selected, they are copied to a temporary location
    // while they're waiting to be printed
    object[,] tempArray = new object[6,3];

    // This stops the while loop once enough credit hours have been taken 
    // It must reach 123 hours for CS Degree .
    int hourCounter = 0;

    int iteration = 0;

    while (hourCounter < 123)
        {
            // this while loop copies some classes from classesArray to tempArray
            // so they can be printed into the excel template (NewStudentTemplateCS.xlsx)
            //
            int classes = 0, hours = 0; // stops while loop if limit is reached
            int w = 0, x = 0; // used to select individual elements of tempArray (0 based)
                              // w = row
                              // x = column
            int y = 1, z = 1; // used to select individual elements of classesArray (1 based)
                              // y = row
                              // z = column
            while(classes < 7 || hours < 17)
            {
                // this loop checks the status of the flag and stops at the first avaliable
                // class/row of classesArray
                while (classesArray[y,7] == (object)1)
                {
                    y++;
                }

                // copies the call EX: "MATH 2313" from classesArray to tempArray
                tempArray[w,x] = classesArray[y,z];
                x += 2;
                z += 2;
                // copies the name EX: "Calculus I" from classesArray to tempArray
                tempArray[w, x] = classesArray[y, z];
                x++;
                z++;
                // Copies the hours EX: "3" from classesArray to tempArray
                tempArray[w, x] = classesArray[y, z];

                Console.WriteLine("debug test");

                // increments classes, hours, and hourCounter for exit decision
                classes += 1;
                hours += (int)classesArray[y, z];
                hourCounter += (int)classesArray[y, z];

                // sets flag to one
                z += 3;
                classesArray[y, z] = 1;

            }// end while loop

            // print method that prints temp array and clears tempArray for next use
            PrintArray(tempArray, iteration, workbook2, excelSheets);

            // iterates iteration
            iteration++;

        } // end while loop
        // print method that prints temp array and clears tempArray for next use
        PrintArray(tempArray, iteration, workbook2, excelSheets);

        // iterates iteration
        iteration++;

    } // end while loop
} // end ProcessObjects method

I have commented out each of the following lines individually, but each line of code all returns the same error I listed above.

我已经分别注释了下面的每一行,但是每一行代码都返回我上面列出的相同错误。

Console.WriteLine("debug test");

// increments classes, hours, and hourCounter for exit decision
classes += 1;
hours += (int)classesArray[y, z];
hourCounter += (int)classesArray[y, z];

// sets flag to one
z += 3;
classesArray[y, z] = 1;

2 个解决方案

#1


3  

Step through your code in the debugger:

在调试器中逐步完成您的代码:

object[,] tempArray = new object[6,3];

You are creating an array with max indexes at tempArray[5, 2]. Then you start looping. At the start of each loop:

您正在使用tempArray[5,2]的最大索引创建一个数组。然后你开始循环。在每个循环的开始:

int w = 0, x = 0;

Then in the body of the loop:

然后在循环体中:

tempArray[w,x] = classesArray[y,z];

You assign to tempArray[0, 0]

赋值给tempArray[0,0]

x += 2;
z += 2;
tempArray[w, x] = classesArray[y, z];

You assign to tempArray[0, 2]

赋值给tempArray[0,2]

x++;
z++;
// Copies the hours EX: "3" from classesArray to tempArray
tempArray[w, x] = classesArray[y, z];

You assign to tempArray[0, 3]. But the maximum index of tempArray is [0, 2]; your array index is out of range, exactly what the exception was telling you.

分配给tempArray[0,3]。但tempArray的最大指标是[0,2];数组索引超出范围,这正是异常告诉您的。

If you can be certain that y and z can never go outside the bounds of classesArray, you can declare tempArray like this:

如果你可以确定y和z不能超出classesArray的界限,你可以这样声明tempArray:

object[,] tempArray = new object[classesArray.GetLength(0), classesArray.GetLength(1)];

But with all those hard-coded magic numbers, and trying to synchronize arrays with differing bases, this is very risky code.

但是对于所有这些硬编码的神奇数字,并且尝试用不同的碱基来同步数组,这是非常危险的代码。

#2


1  

The integers you are using (i.e. w,x,y,z) are becoming bigger than the defined array size. You should add breakpoints into the code so you can see what is happening during compiling and see where they are becoming bigger than expected by the array definitions.

您正在使用的整数(即w、x、y、z)正变得大于定义的数组大小。您应该向代码中添加断点,这样您就可以看到在编译过程中发生了什么,并且可以看到在数组定义中,断点的大小比预期的要大。

The rules of your loop, generally, are used to stop index out of bounds exceptions occurring. I would suggest breaking up the code a bit, there seems to be a lot of stuff going on, but it seems to be too much for this loop.

通常,您的循环规则被用来阻止异常发生的索引。我建议把代码分解一下,似乎有很多事情要做,但是对于这个循环来说似乎太多了。

#1


3  

Step through your code in the debugger:

在调试器中逐步完成您的代码:

object[,] tempArray = new object[6,3];

You are creating an array with max indexes at tempArray[5, 2]. Then you start looping. At the start of each loop:

您正在使用tempArray[5,2]的最大索引创建一个数组。然后你开始循环。在每个循环的开始:

int w = 0, x = 0;

Then in the body of the loop:

然后在循环体中:

tempArray[w,x] = classesArray[y,z];

You assign to tempArray[0, 0]

赋值给tempArray[0,0]

x += 2;
z += 2;
tempArray[w, x] = classesArray[y, z];

You assign to tempArray[0, 2]

赋值给tempArray[0,2]

x++;
z++;
// Copies the hours EX: "3" from classesArray to tempArray
tempArray[w, x] = classesArray[y, z];

You assign to tempArray[0, 3]. But the maximum index of tempArray is [0, 2]; your array index is out of range, exactly what the exception was telling you.

分配给tempArray[0,3]。但tempArray的最大指标是[0,2];数组索引超出范围,这正是异常告诉您的。

If you can be certain that y and z can never go outside the bounds of classesArray, you can declare tempArray like this:

如果你可以确定y和z不能超出classesArray的界限,你可以这样声明tempArray:

object[,] tempArray = new object[classesArray.GetLength(0), classesArray.GetLength(1)];

But with all those hard-coded magic numbers, and trying to synchronize arrays with differing bases, this is very risky code.

但是对于所有这些硬编码的神奇数字,并且尝试用不同的碱基来同步数组,这是非常危险的代码。

#2


1  

The integers you are using (i.e. w,x,y,z) are becoming bigger than the defined array size. You should add breakpoints into the code so you can see what is happening during compiling and see where they are becoming bigger than expected by the array definitions.

您正在使用的整数(即w、x、y、z)正变得大于定义的数组大小。您应该向代码中添加断点,这样您就可以看到在编译过程中发生了什么,并且可以看到在数组定义中,断点的大小比预期的要大。

The rules of your loop, generally, are used to stop index out of bounds exceptions occurring. I would suggest breaking up the code a bit, there seems to be a lot of stuff going on, but it seems to be too much for this loop.

通常,您的循环规则被用来阻止异常发生的索引。我建议把代码分解一下,似乎有很多事情要做,但是对于这个循环来说似乎太多了。