索引超出范围异常。作为索引的第330行超出了数组的界限!

索引超出范围异常。作为索引的第330行超出了数组的界限!

问题描述:

public Int64 DoIt()
{
    int Totaldem = Convert.ToInt32(cmdGetTotaldem.ExecuteScalar());
    int SegmentCount = Convert.ToInt32(cmdGetSegmentCount.ExecuteScalar());
    float CrossEl = Convert.ToSingle(cmdGetCrossElasticity.ExecuteScalar());
    float ServiceEl = Convert.ToSingle(cmdGetServiceElasticity.ExecuteScalar());
    int ServiceInitial = Convert.ToInt32(cmdGetTPHPresently.ExecuteScalar());
    int ServiceFinal = Convert.ToInt32(cmdGetTPHAfterIncrease.ExecuteScalar());

    SqlDataReader drGetSegmentShares = cmdGetSegmentShares.ExecuteReader();
    SqlDataReader drGetSegmentElasticity = cmdGetSegmentElasticity.ExecuteReader();
    SqlDataReader drGetFare = cmdGetFare.ExecuteReader();

    string FlatfareStr;
    float Flatfare = 0;
    string PeakfareStr;
    float Peakfare = 0;
    string NonPeakfareStr;
    float NonPeakfare = 0;

    while (drGetFare.Read())
    {
        FlatfareStr = drGetFare[0].ToString();
        Flatfare = Convert.ToSingle(FlatfareStr);
        PeakfareStr = drGetFare[1].ToString();
        Peakfare = Convert.ToSingle(PeakfareStr);
        NonPeakfareStr = drGetFare[2].ToString();
        NonPeakfare = Convert.ToSingle(PeakfareStr);
    }

    float totalCal = 0;
    float loopCal = 0;
    Int64 ProDem = 0;
    int[] share = new int[10];
    string shareStr;
    string shareElStr;
    float[] shareEl = new float[10];

    drGetSegmentShares.Read();
    drGetSegmentElasticity.Read();

    for (int i = 0; i < SegmentCount; i++)
    {

        shareStr = drGetSegmentShares[i].ToString();          //LINE  330
        share[i] = int.Parse(shareStr) * Totaldem / 100;
        shareElStr = drGetSegmentElasticity[i].ToString();
        shareEl[i] = Convert.ToSingle(shareElStr);             

        loopCal = loopCal + (share[i] * (1 + shareEl[i] * ((Peakfare - Flatfare) *                     Flatfare) - CrossEl + ((Peakfare - NonPeakfare) * NonPeakfare)));

   }
   
    totalCal = loopCal;
    ProDem = Convert.ToInt64(totalCal);
    con.Close();
    return ProDem;

}





我的尝试:



我的第一个循环顺利但是在第二个循环中它在第330行抛出错误(提到)。

我已声明尺寸&适当的数据类型然后导致错误的原因??

SegmentCount的值是6,总是小于10。

帮帮我.. !!



What I have tried:

I first loop goes well but in second loop it throws error at line 330 (mentioned).
i've declare size & appropriate datatype then what caused the error??
"Value of SegmentCount is 6, and always less than 10".
Help me out..!!

当你试图将数据插入到数组中时,你应该知道数组的一侧是否正确,它将在你的情况下给出异常你声明float [] shareEl = new浮[10];它允许你只插入数据10次,如果你的数据超过10就会出错,所以我认为在你的SegmentCount中计数超过10,



所以你可以在声明数组

时加上那个数试试吧



float [] shareEl = new float [SegmentCount];



它会帮到你。 。
when you trying to insert data in to the array that time you should know the side of array exactly other wise it will give exception in your case you declare float[] shareEl = new float[10]; it allow you to insert data only for 10 time bu if your data is more than 10 it will give error, so I think in your "SegmentCount" the count is more than 10,

so you can put that count at the time of declaration of array
e.g. try it

float[] shareEl = new float[SegmentCount];

it will help you..


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Quote:

索引超出范围异常。第330行作为索引在数组的边界之外!

Index out of range exception. line 330 as index was outside the bound of array!

只有一个解释,索引指向数组的结尾。

当你声明一个大小为10的数组时,允许index从0开始,到9结束,超出这些值的任何索引超出范围。

There is only one explanation, The index is pointing after the end of the array.
When you declare an array of size 10, allowed index start at 0 and end at 9, any index outside of these values are out of range.


假设第330行是指示的行:

Assuming line 330 is the line indicated:
shareStr = drGetSegmentShares[i].ToString();



然后错误的原因是由表示的SQL命令cmdGetSegmentShares 未返回与 cmdGetSegmentCount 返回的值一样多的列。您正在尝试访问该命令返回的数据的 i 列,并且该列不存在。



您需要调试查询以确定为什么它没有返回任意数量的列。


then the cause of the error is that the SQL command represented by cmdGetSegmentShares is not returning as many columns as the value returned by cmdGetSegmentCount. You're trying to access the ith column of the data returned by that command, and that column doesn't exist.

You need to debug your query to work out why it's not returning as many columns as you expect.