C# foreach 在 foreach 循环中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/331607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# foreach within a foreach loop
提问by Asrrin29
I don't have too much experience with C# so if someone could point me in the right direction I would greatly appreciate it. I have a foreach loop that references a variable of an object. I wish to make another foreach loop inside the main one that compares (or performs actions on) the current variable to the rest of the variables in the array of the object. I have the following code:
我对 C# 没有太多经验,所以如果有人能指出我正确的方向,我将不胜感激。我有一个引用对象变量的 foreach 循环。我希望在主循环中创建另一个 foreach 循环,将当前变量与对象数组中的其余变量进行比较(或对其执行操作)。我有以下代码:
// Integrate forces for each body.
foreach (RigidBodyBase body in doc.Bodies)
{
// Don't move background-anchored bodies.
if (body.anchored) continue;
// This is where we will add Each Body's gravitational force
// to the total force exerted on the object.
// For each other body, get it's point and it's mass.
// Find the gravitational force exterted between target body and looped body.
// Find distance between bodies.
// vector addition
// Force = G*mass1*mass2/distance^2
// Find vector of that force.
// Add Force to TotalGravityForce
// loop until there are no more bodies.
// Add TotalGravityForce to body.totalForce
}
采纳答案by Charles Bretana
Each time you execute foreach, (even while nesting them) the internal Enumerator should "new" up a new iterator for you, there should not be any issue with that. The issues come about when you are adding, or removing items from the collection while you are still iterating...
每次执行 foreach 时(即使在嵌套它们时)内部 Enumerator 都应该为您“新建”一个新的迭代器,这应该没有任何问题。当您仍在迭代时从集合中添加或删除项目时会出现问题......
Remember, in the inner foreach, to check to make sure you are not on the same item as the outer for each is on
请记住,在内部 foreach 中,要检查以确保您与外部 foreach 不在同一项目上
foreach( RigidBodyBase body in doc.Bodies)
foreach ( RigidBodyBase otherBody in doc.Bodies)
if (!otherBody.Anchored && otherBody != body) // or otherBody.Id != body.Id -- whatever is required...
// then do the work here
by the way, the best place to put this code would be in a GravityForce property of the RigidBodyBase class, Then you could just write:
顺便说一句,放置此代码的最佳位置是 RigidBodyBase 类的 GravityForce 属性,然后您可以编写:
foreach (RigidBodyBase body in doc.Bodies)
body.TotalForce += body.GravityForce;
although depending on what you're doing here (moving all objects?) they're may be even more opportunity for refactoring... I'd also consider having a separate property for the "other" forces, and have TotalForce Property do the summing of the Gravity Force and the "other" Forces?
尽管取决于您在这里做什么(移动所有对象?),它们可能有更多的重构机会......我还考虑为“其他”力量设置一个单独的属性,并让 TotalForce 属性执行重力和“其他”力的总和?
回答by Kibbee
In this case, it's probably better to use a regular for loop with an index to what element you are on. Trying to iterate over the same collection withing it's own foreach loop will cause problems.
在这种情况下,使用带有索引的常规 for 循环可能更好。尝试使用它自己的 foreach 循环迭代同一个集合会导致问题。
回答by Vilx-
IMHO it should be possible, although you should really consider Kibbee's suggestion. Maybe you can optimize it that way too (for example, like this:)
恕我直言,这应该是可能的,尽管您应该真正考虑 Kibbee 的建议。也许你也可以这样优化它(例如,像这样:)
int l = doc.Bodies.Count;
for ( int i = 0; i < l; i++ )
for ( int j = i + 1; j < l; j++ )
// Do stuff
回答by Joshua Hudson
I don't see a problem with this as long as you don't change doc.Bodies inside the inner loop, as this would cause things to blow up. But in theory this would work. As for optimization I'm not sure if it is best, but it is possible.
只要您不更改内部循环内的 doc.Bodies,我就看不出有什么问题,因为这会导致事情崩溃。但理论上这会奏效。至于优化,我不确定它是否是最好的,但这是可能的。
回答by Charles Graham
Well, this is a O(n^2) algorithm, but I guess you have no choice here. How about encapsulating more of your logic into another method. This makes the thing just plain old more readable.
嗯,这是一个 O(n^2) 算法,但我想你在这里别无选择。如何将更多的逻辑封装到另一种方法中。这使得简单的旧事物更具可读性。
foreach (RigidBodyBase body in doc.Bodies)
{
Integrateforces(ref body, Bodies);
}
...
public void Integrateforces(RigidBodyBase out body, RigidBodyBase[] Bodies)
{
//Put your integration logic here
}