简体   繁体   English

每次迭代后如何增加数组大小或释放内存。 错误:索引超出数组C#的范围

[英]how to increase the size of array or free the memory after each iteration. Error: Index was outside the bounds of the array c#

I read data from a text file which is 27 MB file and contains 10001 rows, I need to handle large data. 我从27 MB的文本文件读取数据,该文本文件包含10001行,我需要处理大数据。 I perform some kind of processing in each row of data and then write it back to a text file. 我对每一行数据执行某种处理,然后将其写回到文本文件中。 This is the code I have am using 这是我正在使用的代码

StreamReader streamReader = System.IO.File.OpenText("D:\\input.txt");
        string lineContent = streamReader.ReadLine();
        int count = 0;
        using (StreamWriter writer = new StreamWriter("D:\\ft1.txt"))
        {

            do
            {
                if (lineContent != null)
                {
                    string a = JsonConvert.DeserializeObject(lineContent).ToString();
                    string b = "[" + a + "]";
                    List<TweetModel> deserializedUsers = JsonConvert.DeserializeObject<List<TweetModel>>(b);
                    var CreatedAt = deserializedUsers.Select(user => user.created_at).ToArray();

                    var Text = deserializedUsers.Where(m => m.text != null).Select(user => new
                    {
                        a = Regex.Replace(user.text, @"[^\u0000-\u007F]", string.Empty)
                        .Replace(@"\/", "/")
                        .Replace("\\", @"\")
                        .Replace("\'", "'")
                        .Replace("\''", "''")
                        .Replace("\n", " ")
                        .Replace("\t", " ")
                    }).ToArray();
                    var TextWithTimeStamp = Text[0].a + " (timestamp:" + CreatedAt[0] + ")";
                    writer.WriteLine(TextWithTimeStamp);
                }
                lineContent = streamReader.ReadLine();

            }
            while (streamReader.Peek() != -1);
            streamReader.Close();

This code helps does well up to 54 iterations as I get 54 lines in the output file. 当我在输出文件中获得54行时,此代码最多可以完成54次迭代。 After that it gives error "Index was outside the bounds of the array." 此后,它给出错误“索引在数组的边界之外”。 at line 在线

var TextWithTimeStamp = Text[0].a + " (timestamp:" + CreatedAt[0] + ")";

I am not very clear about the issue if the maximum capacity of array has been violated, if so how can I increase it or If I can write the individual line encountered in loop through 我对这个问题不太清楚,是否违反了数组的最大容量,如果可以的话,如何增加它,或者如果我可以写出循环中遇到的单个行

writer.WriteLine(TextWithTimeStamp);

And clean the storage or something that can solve this issue. 并清洁存储设备或可以解决此问题的设备。 I tried using list insead of array , still issue is the same.Please help. 我尝试使用array的列表insead,仍然问题是相同的。请帮助。

Change this line 更改此行

var TextWithTimeStamp = Text[0].a + " (timestamp:" + CreatedAt[0] + ")";

to

var TextWithTimeStamp = (Text.Any() ? Text.First().a : string.Empty) + 
            " (timestamp:" + (CreatedAt.Any() ? CreatedAt.First() : string.Empty) + ")";

As you are creating Text and CreatedAt collection objects, they might be empty (0 total item) based on some scenarios and conditions. 在创建TextCreatedAt集合对象时,根据某些方案和条件,它们可能为空(共0个项目)。

Those cases, Text[0] and CreatedAt[0] will fail. 在这些情况下, Text[0]CreatedAt[0]将失败。 So, before using the first element, check if there are any items in the collection. 因此,在使用第一个元素之前,请检查集合中是否有任何项目。 Linq method Any() is used for that purpose. Linq方法Any()用于此目的。

Update 更新资料

If you want to skip the lines that do not contain text, change this lines 如果要跳过不包含文本的行,请更改此行

var TextWithTimeStamp = Text[0].a + " (timestamp:" + CreatedAt[0] + ")";
writer.WriteLine(TextWithTimeStamp);

to

if (Text.Any())
{
    var TextWithTimeStamp = Text.First().a + " (timestamp:" + CreatedAt.First() + ")";
    writer.WriteLine(TextWithTimeStamp);
}

Update 2 更新2

To include all the strings s from CreatedAt rather than only the first one, you can add all the values in comma separated strings. 要包括来自CreatedAt所有strings而不是仅包括第一个strings ,可以将所有值添加到逗号分隔的字符串中。 A general example 一个一般的例子

var strings = new List<string> { "a", "b", "c" };
var allStrings = string.Join(",", strings); //"a,b,c"

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

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