简体   繁体   English

c#中的java等效Iterator.remove

[英]java equivalent Iterator.remove in c#

The code has migrated from Java to C #.代码已从 Java 迁移到 C#。

Java code : Java代码:

for (List<ItemNY> level : highU.getLevels())
{
    Iterator<ItemNY> iterItemset = level.iterator();
    while(iterItemset.hasNext())
    {
        ItemNY c = iterItemset.next();

        // Code... 

        if(c.getU() < min)
            {
            iterItemset.remove();
            highU.decreaseCount();
            }
     }
}

C# code : (Code written after convert) C# 代码:(转换后编写的代码)

foreach (List<ItemNY> level in highU.getLevels())
{
    ItemNY c;
    using (IEnumerator<ItemNY> iterItemset = level.GetEnumerator())
    {
        while (iterItemset.MoveNext())
        {
            c= iterItemset.Current;
            //CODE

            foreach (TransactionTP transaction in database.getTransactions())
            {
                int transactionUtility = 0;
                int matchesCount = 0;
                for (int i = 0; i < transaction.size(); i++)
                {
                    if (c.getItems().Contains(transaction.get(i).item))
                    {
                        transactionUtility += transaction.getItemsUtilities()[i].utility;
                        matchesCount++;
                    }
                }
                if (matchesCount == c.size())
                {
                    c.incrementUtility(transactionUtility);
                }
            }

            //END CODE

            if (c.getU() < min)
            {
                iterItemset.remove();  //ERROR
                highU.decreaseCount();
            }
        }
    }
}

I want to remove an item from IEnumerator, how can I do this?我想从 IEnumerator 中删除一个项目,我该怎么做?

Note : The code written in the C # section is more complete, More code is written in the (CODE) to (END CODE) section.注意: C#部分写的代码比较完整,更多的代码写在(CODE)到(END CODE)部分。

I want to remove an item from IEnumerator, how can I do this?我想从 IEnumerator 中删除一个项目,我该怎么做?

Easy: you don't.很简单:你没有。

I do not know Java, but that idea does not make sense in .NET.我不懂 Java,但这个想法在 .NET 中没有意义。 As you can read in the documentation :正如您在文档中所读到

Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.枚举器可用于读取集合中的数据,但不能用于修改底层集合。

As it is also mentioned, foreach should be used instead of IEnumerator<T> directly:正如还提到的,应该直接使用foreach而不是IEnumerator<T>

foreach (List<ItemNY> level in highU.getLevels())
{
    foreach (var item in level)
    {
        if (item.getU() < min)
        {
            level.Remove(item);  //Error, you are modifying a collection being enumerated
            highU.decreaseCount();
        }
    }
}

You would have to put the elements that match the filter in a new collection so you could remove them from level .您必须将与过滤器匹配的元素放在一个新集合中,以便您可以将它们从level删除。

foreach (List<ItemNY> level in highU.getLevels())
{
    var filteredItems = new List<ItemNY>();

    foreach (var c in level)
    {
        foreach (TransactionTP transaction in database.getTransactions())
        {
            int transactionUtility = 0;
            int matchesCount = 0;
            for (int i = 0; i < transaction.size(); i++)
            {
                if (c.getItems().Contains(transaction.get(i).item))
                {
                    transactionUtility += transaction.getItemsUtilities()[i].utility;
                    matchesCount++;
                }
            }

            if (matchesCount == c.size())
            {
                c.incrementUtility(transactionUtility);
            }
        }

        if (c.getU() < min)
        {
            filteredItems.Add(c);
            highU.decreaseCount();
        }
    }

    foreach (var item in filteredItems)
    {
        level.Remove(item);
    } 
}

If the true goal is to remove items from the list then I would use List.RemoveAll , with conditions defining which to remove.如果真正的目标是从列表中删除项目,那么我将使用List.RemoveAll ,并使用条件定义要删除的项目。

If you need the extra counter update then you take a size before and after.如果您需要额外的计数器更新,那么您可以在前后取一个尺寸。 Though at least in the presented case there is no need for the decreaseCount method, as it appears to track size of the list, so I would drop the method, and rely on the list Count.尽管至少在目前的情况下不需要减少计数方法,因为它似乎跟踪列表的大小,所以我会放弃该方法,并依赖列表计数。

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

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