简体   繁体   中英

java equivalent Iterator.remove in c#

The code has migrated from Java to C #.

Java code :

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)

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?

Note : The code written in the C # section is more complete, More code is written in the (CODE) to (END CODE) section.

I want to remove an item from IEnumerator, how can I do this?

Easy: you don't.

I do not know Java, but that idea does not make sense in .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 (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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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