简体   繁体   English

使用for循环清除清单

[英]Clearing a list using a for loop

I'm making a Black Jack game, and at the start of every new round I need to clear the list of cards that represents the Player's and the Dealer's hands. 我正在制作二十一点游戏,在每个新回合开始时,我需要清除代表玩家和庄家的手牌的列表。 I used this to do so: 我这样做是为了:

public void ClearPlayerHand()
        {
            for (int i = 0; i < PlayerHand.Count; ++i) 
            {
                PlayerHand.Remove(PlayerHand[i]);
            }
        }

Problem is I always seem to be left with one card left in the list, or I receive an out of bounds error, no matter how I change the value of i, what is the best method of removing all the elements from the PlayerHand? 问题是,我总是似乎只剩下一张牌,或者无论如何更改i的值,我都会收到超出范围的错误,从PlayerHand中删除所有元素的最佳方法是什么?

If your collection PlayerHand implements ICollection<T> you can just call the .Clear() method. 如果您的集合PlayerHand实现ICollection<T> ,则可以仅调用.Clear()方法。

A common implementation of this interface is List<T> . 此接口的常见实现是List<T>

If you do want to clear a List<T> via a for loop, you should use a reverse for loop. 如果确实要通过for循环清除List<T> ,则应使用反向for循环。 The reason for this is that as you remove an item from the list, it will shift all the index's down one, and you could easy run into index out of bounds exceptions. 这样做的原因是,当您从列表中删除一个项目时,它将使所有索引的下移位置,并且您很容易遇到超出范围的异常。

An example of this would be: 例如:

        for (int i = PlayerHand.Count - 1; i >= 0; i--)
        {
            PlayerHand.RemoveAt(i);
        }

The other answers are right: use Clear . 其他答案是正确的:使用Clear

But, if you wanted to do this with a loop and Remove calls, here's how you would do it: 但是,如果通过循环和“ Remove调用来执行此操作,请按以下步骤操作:

for(int i = PlayerHand.Count - 1; i >= 0; i--)
{
  PlayerHand.RemoveAt(i);
}

Reversing the direction of the iteration is the real trick. 反转迭代的方向是真正的技巧。

This is the best/easiest way to do it. 这是最好/最简单的方法。

PlayerHand.Clear();

Reason for out of bounds 超出范围的原因

As for why you are receiving the out of bounds exception, it's happening because you're removing elements from the list but continually counting up. 至于为什么收到超出范围的异常,这是因为您要从列表中删除元素而不断增加计数。 You would want the last operation to remove i = 0 but it keeps counting. 您可能希望最后一个操作删除i = 0但它一直在计数。

Say PlayerHand has 3 items in it, the following occurs: 假设PlayerHand中有3个项目,则会发生以下情况:

i = 0
remove PlayerHand[0] (it now contains 2 elements)
i = 1
remove PlayerHand[1] (it now contains 1 element)
i = 2
remove PlayerHand[2] (this throws an exception as only PlayerHand[0] exists)

Normally you would count backwards in this case: 在这种情况下,通常情况下您会倒数:

for (int i = PlayerHand.Count - 1; i >= 0; i--) 

Another suggested approach beside Clear method, you can also use RemoveAll to either remove all or part of list 除了Clear方法之外,另一种建议的方法是,您还可以使用RemoveAll删除全部或部分列表

// Remove all items
PlayerHand.RemoveAll(x => true);

// Remove part of list
PlayerHand.RemoveAll(x => ConditionMethod(x));

Alternatively, you can consider using data binding and then you should update the ItemSource, instead of directly manipulating the listbox or listview items. 或者,您可以考虑使用数据绑定,然后应该更新ItemSource,而不是直接操作列表框或listview项。

List<T> SomeSource=...
PlayHand.ItemSource=SomeSource;
SomeSource.Clear();

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

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