简体   繁体   English

为什么我的数组抛出超出范围异常错误?

[英]Why is my array throwing Out of Range Exception Error?

Why does the following code throw an exception? 为什么以下代码会引发异常?

for (int i = 0; i <= Items.Length-1; i++)
{
    Console.WriteLine(Items[i,1]);
}

Exception: 例外:

System.IndexOutOfRangeException was unhandled
  Message="Index was outside the bounds of the array."
  Source="Es"
  StackTrace:
       at Es.Program.Main(String[] args) in C:\Users\Fero\Documents\Visual Studio 2005\Projects\Es\Es\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

Declaration of Items: 物品申报单:

Function which gets the array of strings: 获取字符串数组的函数:

static string[,] ReadFromFile(string filename, int rowsF)
{
    StreamReader SR;
    string S;
    string[] S_split;

    SR = File.OpenText(filename);
    S = SR.ReadLine();

    string[,] myItems = new String[rowsF, 2];
    int row_number = 0;
    while (S != null)
    {
        S_split = S.Split('"');
        //temp_items[row_number,0] = 
        myItems[row_number,0] = S_split[1];
        myItems[row_number,1] = S_split[2];

        row_number++;
        S = SR.ReadLine();
    }
    SR.Close();
    return myItems;
}

string[,] Items = ReadFromFile(myFile, rowsF);

You have a straight two-dimensional array. 您有一个直的二维数组。 Length gives you the total number of elements in the array, but you're using it to calculate the index for a single dimension. 长度为您提供了数组中元素总数 ,但是您使用它来计算单个维度的索引。 What you want is: 您想要的是:

for (int i = 0; i < Items.GetLength(0); i++)
{
    Console.WriteLine(Items[i,1]);
}

Check the length of Items[i]. 检查项目的长度[i]。 It appears to be a 2D array, and it's apparently not null, because you'd get a different exception for that, so it probably is just an empty array at Items[i], or only contains one item. 它似乎是一个2D数组,并且显然不是null,因为您可能会得到一个不同的异常,因此它可能只是Items [i]上的一个空数组,或者只包含一个项目。

Check for: 检查:

Items[i] == null
Items[i].Length > 0

EDIT : Your additional code helped. 编辑 :您的其他代码帮助。 When you split the string to initialize Items, for the item that's giving you trouble, check what you're storing at index 1. Other than that, I can't see a problem with it. 当您分割字符串以初始化Items时,对于给您带来麻烦的项目,请检查您在索引1中存储的内容。除此之外,我看不到它有问题。

It's throwing an exception because Items[i].Length is < 2 因为Items[i].Length <2而引发异常

you need to check that (Items[i].Length >= 2) before trying to access Items[i,1] 您需要先检查(Items[i].Length >= 2)然后再尝试访问Items[i,1]

From what I see, there's a small chance one of the lines doesn't follow the format you require. 从我所看到的情况来看,其中几行不太可能遵循您要求的格式。 It looks like you're retrieving text based on Quote (") delimitation, and S_split[] might not always have those number of fields. 似乎您正在基于引号(“)分隔检索文本,并且S_split []可能并不总是具有这些字段数。

A blank line at the end of the file, for example, will trigger the exception. 例如,文件末尾的空白行将触发异常。

  S_split = S.Split('"'); //temp_items[row_number,0] = myItems[row_number,0] = S_split[1]; myItems[row_number,1] = S_split[2]; 

Are you sure you don't want S_Split[0] & S_Split[1]? 确定要不要S_Split [0]和S_Split [1]吗?

Should see the input file. 应该看到输入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM