指数超出范围。必须是非负的且小于集合的大小? [英] Index was out of range. Must be non-negative and less than the size of the collection??

查看:500
本文介绍了指数超出范围。必须是非负的且小于集合的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样做很简单,我得到这个错误。当我将TRUE更改为FALSE时它不会给出错误,但测试真正的错误...但我需要通过测试,我不明白..有人可以帮忙吗? C#visual studio 2010 NUnit

im doing this simple for and i get this error. When i change TRUE to FALSE it doesnt give that error, but the test real error... but i need the test to pass and i dont understand.. can anybody help? C# visual studio 2010 NUnit

[Test]

public void prueba1()
{

    List<int> lista1 = new List<int>();

    lista1.Add(1);

    lista1.Add(2);

    lista1.Add(3);

    for (int i = 0; i < lista1.Count; i++)
    {

        Console.WriteLine(lista1[i]);
        Assert.True(lista1[i]<lista1[i+1]);

    }


推荐答案

有效列表中的索引是0到2. lista1.Count 将是3,所以 i 从0变为2。当 i 为2时,您尝试访问 lista1 [i + 1] ,这超出了范围。

The valid indexes into your list are 0 through 2.lista1.Count will be 3, so i goes from 0 to 2. When i is 2, you try to access lista1[i+1], which is out of range.

这篇关于指数超出范围。必须是非负的且小于集合的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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