简体   繁体   English

删除 c# 中字符串列表中的一个元素,给出错误(ArgumentOutOfRangeException)

[英]Removing an element in string list in c#, gives error(ArgumentOutOfRangeException)

I'm trying to filter my string list by getting input, but get this error: ArgumentOutOfRangeException: Index was out of range.我试图通过获取输入来过滤我的字符串列表,但出现此错误:ArgumentOutOfRangeException:索引超出范围。 Must be non-negative and less than the size of the collection.必须为非负数且小于集合的大小。 Parameter name: index参数名称:索引

private List<string> title = new List<string>();
private int cellNumber { get; set; }
private int counter = 0;

inputField.onValueChanged.AddListener(delegate { Search(cellNumber-counter); });

public void Search(int size)
{
    for (int i = 0; i < size; i++)
    {

        if (!title[i].ToLower().Contains(inputField.text.ToLower()) && inputField.text != "")
        {
            title.RemoveAt(i);
            counter++;
        }

    }
}

If you have hits your list size decreases for each removed item.如果您点击了每个删除的项目,您的列表大小就会减少。

So even assuming you are passing in the correct size of the list, the iterator variable i at some point at the end runs longer than the new smaller size of your list after removing items => ArgumentOutOfRangeException!因此,即使假设您传递的列表size正确,迭代器变量i在最后某个时刻运行的时间也比删除项目后列表的新较小大小长 => ArgumentOutOfRangeException!


What you could do is keep updating the size and skip i++ whenever you remove an item like eg您可以做的是不断更新大小并在删除诸如 eg 之类的项目时跳过i++

public void Search()
{
    if (string.IsNullOrWhiteSpace(inputField.text))
    {
        return;
    }

    var size = title.Count;
    var i = 0;

    while(i < size)
    {
        if (!title[i].ToLower().Contains(inputField.text.ToLower()))
        {
            title.RemoveAt(i);
            size--;
        }
        else
        {
            i++;
        }
    }
}

Note that there is a simple method that already covers this without further effort: List<t>.RemoveAll请注意,有一个简单的方法已经涵盖了这一点,无需进一步努力: List<t>.RemoveAll

public void Search()
{
    if(string.IsNullOrWhiteSpace(inputField.text))
    {
        return;
    }

    // remove all matches
    title.RemoveAll(t => !t.ToLower().Contains(inputField.text.ToLower()));
}

Though you might be even more interested in using Linq Where in order to return a new collection with the hits without modifying the original one尽管您可能对使用Linq Where更感兴趣,以便在不修改原始集合的情况下返回包含匹配项的新集合

using System.Linq;

...

public string[] Search()
{
    if(string.IsNullOrWhiteSpace(inputField.text))
    {
        return new string[0];
    }

    // remove all matches
    return title.Where(t => t.ToLower().Contains(inputField.text.ToLower())).ToArray();
}

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

相关问题 编写C#列表时出现ArgumentOutOfRangeException <string> 到多个文本框 - ArgumentOutOfRangeException when writing C# List<string> to multiple textbox ArgumentOutOfRangeException 是未处理的 C# 错误 - ArgumentOutOfRangeException was Unhandled C# error 遍历数组后尝试从列表中获取随机元素时出现ArgumentOutOfRangeException错误Unity3D C# - ArgumentOutOfRangeException error Unity3D C# when trying to get a random element from a list after iterating through an array 在C#中使用子字符串函数会导致ArgumentOutOfRangeException异常 - Using substring function in C# gives ArgumentOutOfRangeException exception C#中删除字符串数组的最后一个元素? - C# Removing Last Element of a string Array? C# 将动态创建的对象添加到列表<dynamic> (WinForms 应用程序)错误:System.ArgumentOutOfRangeException</dynamic> - C# Adding dynamically created objects to List<dynamic> (WinForms app) ERROR: System.ArgumentOutOfRangeException c#ArgumentOutOfRangeException - c# ArgumentOutOfRangeException DataGridView ArgumentOutOfRangeException C# - DataGridView ArgumentOutOfRangeException C# Selenium C# - 从下拉列表中选择一个元素会出现错误“元素应该被选择但被 img” - Selenium C# - Selecting an element from a Dropdown list gives error "Element Should have been select but was img" dataGridView中的C#ArgumentOutOfRangeException - C# ArgumentOutOfRangeException in dataGridView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM