开发者

how can modify or add new item into generic list of strings

开发者 https://www.devze.com 2023-01-01 07:43 出处:网络
i want to remove some pretty words in list of words. public System.String CleanNoiseWord(System.String word)

i want to remove some pretty words in list of words.

public System.String CleanNoiseWord(System.String word)
{
    string key = word;
    if (word.Length <= 2)
        key = System.String.Empty;
    else 
        key = word;
    //other validation here
    return key;
}

public IList<System.String> Clean(IList<System.String> words)
{
    var oldWords = words;
    IList<System.String> newWords = new string[oldWords.Count()];
    string key;
    var i = 0;
    foreach (System.String word in oldWords)
    {
        key = this.CleanNoiseWord(word);
        if (!string.IsNullOrEmpty(开发者_运维知识库key))
        {
            newWords.RemoveAt(i);
            newWords.Insert(i++, key);
        }
    }
    return newWords.Distinct().ToList();
}

but i can't add, remove or insert any thing in list! and exception NotSupportedException occured >> Collection was of a fixed size. how i can modify or add new item into generic list of strings?


Of course LINQ could make it nice and easy:

return words.Where(p => !string.IsNullOrEmpty(CleanNoiseWord(p))
            .Distinct()
            .ToList();

Of course we could take this one step further and inline the function call to CleanNoiseWord and drastically simplify your Clean method:

public IList<System.String> Clean(IList<System.String> words)
{
    return words.Where(p => !string.IsNullOrEmpty(p) && p.Length > 2)
                .Distinct()
                .ToList();
}

If no words meet the criteria in the predicate then an empty list will be returned. If you are passing a very large list and want to lazy evaluate it (MSDN) then remove the ToList() from the end - this way actual evaluation of the list will not be performed until you convert it to an actual list. (To be fair, here's a blog post about some of the gotchas of lazy (deferred) evaluation).


I suggest that you create a method

  bool IsNoiseWord(string word) 

and do this:

  words.RemoveAll(IsNoiseWord);

edit: this will only work for actual lists, otherwise

 return words.Where(x=>!IsNoiseWord(x)).Distinct().ToList()


As already said, array is a fixed size list. Use the List instead.

IList<string> newWords = new List<string>(oldWords);


Your code is wrong. Instead of

IList<System.String> newWords = new string[oldWords.Count()];

Make this

IList<System.String> newWords = new List<String>();

You do not need to initialize to a certain size with a generic list.


You cannot insert items into a fixed size list, a happy medium would be to create a new list and insert as you "clean".


Following Mike's answer,

public bool IsNoise(String word) 
{     
    return (word.Length <= 2) && validation2 && validation3;    
} 

public List<String> Clean(List<String> words) 
{ 
    words.RemoveAll(IsNoise);
    return words;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号