C#:使用foreach或for循环从ArrayList中删除项目? [英] C#: remove items from an ArrayList with foreach or for loop?

查看:757
本文介绍了C#:使用foreach或for循环从ArrayList中删除项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#编程和C#编程都相当了解(我之前已经学过一些基本的Java).我正在尝试在 Unity3D 中使用C#,但我有一个问题:

I am quite a noob both with programming and programming with C# (I have learned some basic Java before). I am trying to play around with C# in Unity3D, and I have a question:

使用for循环而不是foreach迭代来删除ArrayList中的任何项更好吗?

两者似乎都对我有用. Foreach产生的编码稍少,但讨论 互联网似乎更喜欢循环.哪个更好,为什么?

Both seems to work for me. Foreach produce slightly less coding but the discussions on the Internet seems to be favoring for-loop. Which is better and why?

for循环版本:

public void RemoveUnitFromList(GameObject Unit) {
    if (SelectedUnitList.Count > 0) {
        for (int i = 0; i < SelectedUnitList.Count; i++) {
            GameObject ArrayListUnit = SelectedUnitList[i] as GameObject;
            if (ArrayListUnit == Unit) {
                SelectedUnitList.RemoveAt(i);
                // some other codes
            }
        }
    }
}

foreach版本:

foreach version:

public void RemoveUnitFromList(GameObject Unit) {
    if (SelectedUnitList.Count > 0) {
        foreach (GameObject ArrayListUnit in new ArrayList(SelectedUnitList)) {
            if (ArrayListUnit == Unit) {
                SelectedUnitList.Remove(ArrayListUnit);
                // some other codes
            }
        }
    }
}

上面的代码片段中的任何一个对我来说都可以正常工作.我只想研究两者之间的区别. 但是,将in new ArrayList(SelectedUnitList)更改为in SelectedUnitList无效:

Either of the code snippets above works fine for me. I only want to study the difference between the two. However, changing in new ArrayList(SelectedUnitList) to in SelectedUnitList did not work:

public void RemoveUnitFromList(GameObject Unit) {
    if (SelectedUnitList.Count > 0) {
        foreach (GameObject ArrayListUnit in SelectedUnitList) {
            if (ArrayListUnit == Unit) {
                SelectedUnitList.Remove(ArrayListUnit);
                // some other codes
            }
        }
    }
}

Edit2: 上面的代码可能会产生以下错误:

The above code could produce the following error:

InvalidOperationException: List has changed.
System.Collections.ArrayList+SimpleEnumerator.MoveNext () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections/ArrayList.cs:141)
Mouse3.RemoveUnitFromList (UnityEngine.GameObject Unit) (at Assets/Scripts/Mouse3.cs:216)
Mouse3.Update () (at Assets/Scripts/Mouse3.cs:85)

让我解释更多.该方法在游戏中动态地起作用. SelectedUnitList是一个ArrayList,用于收集RTS游戏中的选定单位.该方法从ArrayList中删除取消选择的单位,因此Unit可以为0、1或更大.

Let me explains more. The method functions dynamically in game. The SelectedUnitList is an ArrayList used to collect selected unit in a RTS game. The method removes the deselected units from the ArrayList, so Unit could be 0, 1 or more.

Edit3(上次): 谢谢大家提供的所有信息.我认为评论中的@Seminda回答了我的问题.

Edit3 (last time): Thank you all for all the information provided. I think @Seminda in the comments answered my question.

推荐答案

摘自

foreach 语句用于遍历集合以获取所需的信息,但不能用于在源集合中添加或删除项目,以避免不可预期的副作用. 如果需要从源集合中添加或删除项目,请使用循环 .

The foreach statement is used to iterate through the collection to get the information that you want, but can not be used to add or remove items from the source collection to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop.

参考您的代码,请注意以下几点:

Reference to your code, note following thing:

查看有关 ArrayList 的详细信息.通常最好使用 列表 .列表不仅可以避免装箱或拆箱,而且还可以使代码更清晰,更易于出错.

See details about the ArrayList. It is usually better to use List. Lists not only avoid boxing or unboxing, but they also lead to clearer and less bug-prone code.

更新:

foreach(问题中的第二个片段)起作用是因为您初始化了新的ArrayList.

foreach (2nd snippet in your question) works because you initialize new ArrayList.

foreach (GameObject ArrayListUnit in new ArrayList(SelectedUnitList))

您已经初始化了冗余ArrayList并将其仅用于迭代.但是,当您在foreach循环中从ArrayList中删除时,它是从实际的ArrayList中删除的,即SelectedUnitList.

You have initialized redundant ArrayList and used it just for iteration. But when you remove from ArrayList within foreach loop it is from actual ArrayList i.e. SelectedUnitList.

混乱:

使用for loop避免冗余并从ArrayList中删除.

Use for loop to avoid redundancy and removing from the ArrayList.

这篇关于C#:使用foreach或for循环从ArrayList中删除项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆