简体   繁体   English

为什么这个随机的Next()抛出异常?

[英]Why does this random Next() throw an exception?

Hi I'm new to programming and am working from this book. 嗨,我是编程新手,正在从本书中学习。 This is an exercise at the end of one of the chapters. 这是其中一章末的练习。 But my code throws this exception. 但是我的代码抛出了这个异常。

        Random rand = new Random();
        List<int> numbers = new List<int>();

        for (int i = 0; i < 1000; i++)
        {
            numbers[i] = rand.Next(1, 1001);
        }

        for (int i = 0; i < numbers.Count; i++)
        {
            listBox1.Items.Add(numbers[i]);
        }

Here is the error: 这是错误: 在此处输入图片说明

numbers is empty, so any index is invalid. numbers为空,因此任何索引均无效。 Either use numbers.Add to add numbers, or better use a pre-sized array. 要么使用numbers.Add添加数字或更好的使用预大小的数组。 Using a List is not necessary here. 此处无需使用List An array is just fine because the final size is known. 数组很好,因为最终大小是已知的。 It is also faster, although that does not seem to be an issue here. 它也更快,尽管在这里似乎不是问题。

ArgumentOutOfRangeException is only called on Random.Next when minValue is greater than maxValue. minValue is greater than maxValue.时,仅在Random.Next调用ArgumentOutOfRangeException minValue is greater than maxValue. as according to: http://msdn.microsoft.com/en-us/library/2dx6wyd4.aspx#ddueExceptionsToggle 如: http : //msdn.microsoft.com/en-us/library/2dx6wyd4.aspx#ddueExceptionsToggle

Your problem lays with numbers[i] as numbers has zero elements, meaning setting numbers[0] = random.Next(1, 1000); 您的问题在于numbers[i]因为数字有零个元素,这意味着设置numbers[0] = random.Next(1, 1000); will return an ArgumentOutOfRangeException because there is no range yet. 将返回ArgumentOutOfRangeException因为尚无范围。 Use numbers.Add(random.Next(1, 1000)); 使用numbers.Add(random.Next(1, 1000)); instead 代替

Just a hint, when working with indexers in array type objects (when you use object[key] ) and for loops you should always use the arrays length as the upper limit of the for loop, like so: 只是一个提示,当在数组类型对象中使用索引器(使用object[key] )和for循环时,应始终将数组长度用作for循环的上限,如下所示:

for(int i = 0; i < 1000 && i < numbers.Count(); i++){
    //Do stuff with i
}

In your case the loop will never happen as i is always going to be smaller then numbers.Count() unless you add items to it 在您的情况下,循环将永远不会发生,因为i总是会比numbers.Count()除非您向其中添加项目

Your numbers List dosent have any elements yet. 您的号码列表中有任何内容。

try this: 尝试这个:

numbers.add(rand.Next(1, 1001));

Try 尝试

numbers.Add(rand.Next(1, 1001));

instead of 代替

numbers[i] = rand.Next(1, 1001);

so you can have a dynamically re-sizing list. 因此您可以拥有一个动态调整大小的列表。

Your list does not have anything at index position i so you receive ArgumentOutOfRangeException . 您的列表在索引位置i上没有任何内容,因此您收到ArgumentOutOfRangeException You can only access an item in a collection via its index if there is something there at the given index. 如果给定索引中有项目,则只能通过其索引访问集合中的项目。

Try with this 

List 清单

   Random rand = new Random();
                List<int> numbers = new List<int>();
                //int [] nums ;
                for (int i = 0; i < 1000; i++)
                {
                    //nums[i] = rand.Next(0, 1000);
                    numbers.Add(rand.Next(0,1000));
                    //numbers[i] = rand.Next(1, 1001);
                }

------------------------OR ------------------------------------------ - - - - - - - - - - - - 要么 - - - - - - - - - - - - - -----------------

You can use Array also 您也可以使用Array

Array 数组

Random rand = new Random();
            List<int> numbers = new List<int>();
            int [] nums = new int [1000] ;
            for (int i = 0; i < 1000; i++)
            {
                nums[i] = rand.Next(0, 1000);                

            }

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

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