&QUOT;指数超出范围。必须为非负且小于collection.\r\\\<br/>Parameter名的大小:指数&QUOT; [英] &quot;Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index&quot;

查看:1566
本文介绍了&QUOT;指数超出范围。必须为非负且小于collection.\r\\\<br/>Parameter名的大小:指数&QUOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关创建0到1之间,并与100个随机数的列表我下面的代码写的,我收到的错误。

 公开名单<浮动>随机的()
{
名单,LT;浮动> storerandomvalues =新的List<浮动>(100);
随机randomvalues =新的随机();
浮动randomnum;
为(INT计数器= 0;反< 100;反++)
{
randomnum = 0F;
randomnum = randomvalues.Next(1);
storerandomvalues [计数器] = randomnum; //错误
}
返回storerandomvalues;
}


解决方案

使用的 列表< T>。新增 方法来代替, storerandomvalues 不是array.You不能与索引项目添加到一个通用的list.You可以使用索引器只能改变现有的。项目

  storerandomvalues.Add(randomnum); 


For creating a list with 100 random number between 0 and 1 I wrote below code that I receive the error.

      public List<float> random()
        {
            List<float> storerandomvalues = new List<float>(100);
            Random randomvalues = new Random();
            float randomnum;
            for (int counter = 0; counter < 100; counter++)
            {
                randomnum = 0f;
                randomnum = randomvalues.Next(1);
                storerandomvalues[counter]= randomnum;           //the error
            }
            return storerandomvalues;
        }

解决方案

Use List<T>.Add method instead, storerandomvalues is not an array.You can't add items with indexer to a generic list.You can use indexer only to change existing items.

storerandomvalues.Add(randomnum);

这篇关于&QUOT;指数超出范围。必须为非负且小于collection.\r\\\<br/>Parameter名的大小:指数&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆