索引超出的数组限制,List有长度限制?

卧槽这是我的昵称吗 2014-03-21 10:05:28
从txt文件里一行一行读取数据并记录一些有用的数据,数据里少的时候没啥事,但刚才读取一个3M的Txt就报错了,调试的信息居然是索引超出了? 有这种事?


StreamReader sr = new StreamReader(file,Encoding.GetEncoding("GBK"));
string temp = null;
while ((temp = sr.ReadLine()) != null && !sr.EndOfStream)
{
PostEntity entity = new PostEntity();
if (temp.Contains("id=") )
{
FillEntity(entity, temp);
list.Add(entity); //调试显示当list.count>5160时报错,异常信息为 索引超出的数组限制
}
}


...全文
1057 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
List<T> 的容量是 List<T> 可以容纳的元素数。当向 List<T> 中添加元素时,将通过重新分配内部数组来根据需要自动增大容量。 如果可以估计集合的大小,那么可以使用 List<T>(Int32) 构造函数并指定初始容量,这样便无需在向 List<T> 中添加元素时执行大量的大小调整操作。 可通过调用 TrimExcess 方法或通过显式设置 Capacity 属性减少容量。减少容量会重新分配内存并复制 List<T> 中的所有元素。 此构造函数的运算复杂度为 O(1)。
u014460264 2014-03-31
  • 打赏
  • 举报
回复
居然是索引超出了? 有这种事?
tiannailu 2014-03-31
  • 打赏
  • 举报
回复
public void Add(T item)
{
    if (this._size == this._items.Length)
    {
        this.EnsureCapacity(this._size + 1);
    }
    this._items[this._size++] = item;
    this._version++;
}

 

 
tiannailu 2014-03-31
  • 打赏
  • 举报
回复
            List<string> stringList = new List<string>();
while (true)
{
stringList.Add("abc");
}

内存都溢出了还是没报错~
应该是有什么地方操作过list吧
失落的神庙 2014-03-31
  • 打赏
  • 举报
回复
是你index 超出了 list长度 大叔
gw6328 2014-03-31
  • 打赏
  • 举报
回复
我感觉是不能过大。 我以前用stack 数据过大也会报错。
wuhaipinm 2014-03-31
  • 打赏
  • 举报
回复
PostEntity 对象代码发来看看
lvfeng19806001 2014-03-31
  • 打赏
  • 举报
回复
引用 13 楼 u011710947 的回复:
我靠。。 是不是string tid = System.Text.RegularExpressions.Regex.Split(str1s[0], "tid=")[1];这个的问题。。。。
如果是FillEntity方法中错误,异常信息应该是在调用这个方法的时候报,而不是add的时候。
  • 打赏
  • 举报
回复
我靠。。 是不是string tid = System.Text.RegularExpressions.Regex.Split(str1s[0], "tid=")[1];这个的问题。。。。
  • 打赏
  • 举报
回复
引用 10 楼 linrachel 的回复:
FillEntity 这个方法发出来看看
(读取的内容是html格式的,从html里面找出几个有用的参数)
   
        private void FillEntity(PostEntity entity, string line)
        {
            if (String.IsNullOrEmpty(line)) return;
			
			//找出url
            string str1 = System.Text.RegularExpressions.Regex.Split(line, "</a></td><td  colspan=2><a href=")[0];
            string[] str1s = System.Text.RegularExpressions.Regex.Split(str1, "target=_blank>");
			//找出url中的tid
            entity.title = str1s[1];
            string tid = System.Text.RegularExpressions.Regex.Split(str1s[0], "tid=")[1];
            entity.tid= Convert.ToInt32(tid.Replace("\"","").TrimEnd());
			//找出postdate
            string[] str2s = System.Text.RegularExpressions.Regex.Split(line, "<td  colspan=2>");
            string time = System.Text.RegularExpressions.Regex.Split(str2s[2], "</td>")[0];
            entity.postDate = Convert.ToDateTime(time);
        }
王子文龙 2014-03-31
  • 打赏
  • 举报
回复
单纯回答你的问题是list没有大小限制
linrachel 2014-03-31
  • 打赏
  • 举报
回复
FillEntity 这个方法发出来看看
ycg_893 2014-03-31
  • 打赏
  • 举报
回复
应该是线程问题,有没有使用静态实例?
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
引用 4 楼 JulioHuang 的回复:

            StreamReader sr = new StreamReader(file,Encoding.GetEncoding("GBK")); 
            string temp = null;
            while ( !sr.EndOfStream &&(temp = sr.ReadLine()) != null)   
            {
                PostEntity entity = new PostEntity();
                if (temp.Contains("id=") )
                {
                    FillEntity(entity, temp);
                    list.Add(entity);  //调试显示当list.count>5160时报错,异常信息为 索引超出的数组限制
                }
            }
看不懂什么错,随便试试这个吧
不管用 而且应该不是读取时的错误吧。。。
黄亮 2014-03-21
  • 打赏
  • 举报
回复

            StreamReader sr = new StreamReader(file,Encoding.GetEncoding("GBK")); 
            string temp = null;
            while ( !sr.EndOfStream &&(temp = sr.ReadLine()) != null)   
            {
                PostEntity entity = new PostEntity();
                if (temp.Contains("id=") )
                {
                    FillEntity(entity, temp);
                    list.Add(entity);  //调试显示当list.count>5160时报错,异常信息为 索引超出的数组限制
                }
            }
看不懂什么错,随便试试这个吧
  • 打赏
  • 举报
回复
引用 1 楼 sbwwkmyd 的回复:
你的list哪里来的?多线程访问要lock
没用到多线程 因为耗时不多 1秒不到的时间 没做那么麻烦
  • 打赏
  • 举报
回复
引用 1 楼 sbwwkmyd 的回复:
你的list哪里来的?多线程访问要lock
list是在读取txt之前new的( List<postentity> lsit = new List<postentity>() )
showjim 2014-03-21
  • 打赏
  • 举报
回复
你的list哪里来的?多线程访问要lock

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧