如何在For Each ... Next循环中获取对象的索引?

时间:2021-07-14 17:28:40

I'm using the following syntax to loop through a list collection:

我使用以下语法循环列表集合:

For Each PropertyActor As JCPropertyActor In MyProperty.PropertyActors

    i = IndexOf(PropertyActor)
Next

How do I get the index of the current object within the loop? I'm using IndexOf(PropertyActor) but this seems inefficient as it searches the collection when I already have the object available!

如何在循环中获取当前对象的索引?我正在使用IndexOf(PropertyActor)但这似乎效率低下,因为当我已经拥有该对象时它会搜索该集合!

6 个解决方案

#1


12  

An index doesn't have any meaning to an IEnumerable, which is what the foreach construct uses. That's important because foreach may not enumerate in index order, if your particular collection type implements IEnumerable in an odd way. If you have an object that can be accessed by index and you care about the index during an iteration, then you're better off just using a traditional for loop:

索引对IEnumerable没有任何意义,这是foreach构造使用的。这很重要,因为如果您的特定集合类型以奇怪的方式实现IEnumerable,则foreach可能无法按索引顺序枚举。如果你有一个可以通过索引访问的对象,并且你在迭代期间关心索引,那么你最好只使用传统的for循环:

for (int i=0;i<MyProperty.PropertyActors.Length;i++)
{
    //...
}

#2


12  

AFAIK since this pulls the object out of the collection, you would have to go back to the collection to find it.

AFAIK因为它将对象拉出集合,你将不得不回到集合中找到它。

If you need the index, rather than using a for each loop, I would just use a for loop that went through the indices so you know what you have.

如果你需要索引,而不是为每个循环使用一个,我只会使用一个遍历索引的for循环,这样你就知道你拥有了什么。

#3


6  

It might be easiest to just keep a separate counter:

保持一个单独的计数器可能是最容易的:

i = 0
For Each PropertyActor As JCPropertyActor In MyProperty.PropertyActors
    ...
    i = i + 1
Next

As an aside, Python has a convenient way of doing this:

顺便说一句,Python有一个方便的方法:

for i, x in enumerate(a):
    print "object at index ", i, " is ", x

#4


2  

just initialize an integer variable before entering the loop and iterate it...

只需在进入循环之前初始化一个整数变量并迭代它...

Dim i as Integer 
For Each PropertyActor As JCPropertyActor In MyProperty.PropertyActors
    i++
Next

#5


1  

Add an index variable that you increase yourself for each iteration?

添加一个自己为每次迭代增加的索引变量?

#6


1  

You could use the "FindIndex" method.

您可以使用“FindIndex”方法。

MyProperty.PropertyActors.FindIndex(Function(propActor As JCPropertyActor) propActor  = JCPropertyActor)

But inside of a for each loop that seems like alot of extra overhead, and seems like the same resulting problem as the "IndexOf" method. I suggest using old fashioned index iteration. This way you have your index and your item.

但是对于每个循环来说,似乎有很多额外的开销,并且看起来像“IndexOf”方法一样产生问题。我建议使用旧式的索引迭代。这样您就拥有了索引和项目。

Dim PropertyActor As JCPropertyActor
For i As Integer = 0 To MyProperty.PropertyActors.Count - 1
    PropertyActor = MyProperty.PropertyActors.Item(i)
Next

#1


12  

An index doesn't have any meaning to an IEnumerable, which is what the foreach construct uses. That's important because foreach may not enumerate in index order, if your particular collection type implements IEnumerable in an odd way. If you have an object that can be accessed by index and you care about the index during an iteration, then you're better off just using a traditional for loop:

索引对IEnumerable没有任何意义,这是foreach构造使用的。这很重要,因为如果您的特定集合类型以奇怪的方式实现IEnumerable,则foreach可能无法按索引顺序枚举。如果你有一个可以通过索引访问的对象,并且你在迭代期间关心索引,那么你最好只使用传统的for循环:

for (int i=0;i<MyProperty.PropertyActors.Length;i++)
{
    //...
}

#2


12  

AFAIK since this pulls the object out of the collection, you would have to go back to the collection to find it.

AFAIK因为它将对象拉出集合,你将不得不回到集合中找到它。

If you need the index, rather than using a for each loop, I would just use a for loop that went through the indices so you know what you have.

如果你需要索引,而不是为每个循环使用一个,我只会使用一个遍历索引的for循环,这样你就知道你拥有了什么。

#3


6  

It might be easiest to just keep a separate counter:

保持一个单独的计数器可能是最容易的:

i = 0
For Each PropertyActor As JCPropertyActor In MyProperty.PropertyActors
    ...
    i = i + 1
Next

As an aside, Python has a convenient way of doing this:

顺便说一句,Python有一个方便的方法:

for i, x in enumerate(a):
    print "object at index ", i, " is ", x

#4


2  

just initialize an integer variable before entering the loop and iterate it...

只需在进入循环之前初始化一个整数变量并迭代它...

Dim i as Integer 
For Each PropertyActor As JCPropertyActor In MyProperty.PropertyActors
    i++
Next

#5


1  

Add an index variable that you increase yourself for each iteration?

添加一个自己为每次迭代增加的索引变量?

#6


1  

You could use the "FindIndex" method.

您可以使用“FindIndex”方法。

MyProperty.PropertyActors.FindIndex(Function(propActor As JCPropertyActor) propActor  = JCPropertyActor)

But inside of a for each loop that seems like alot of extra overhead, and seems like the same resulting problem as the "IndexOf" method. I suggest using old fashioned index iteration. This way you have your index and your item.

但是对于每个循环来说,似乎有很多额外的开销,并且看起来像“IndexOf”方法一样产生问题。我建议使用旧式的索引迭代。这样您就拥有了索引和项目。

Dim PropertyActor As JCPropertyActor
For i As Integer = 0 To MyProperty.PropertyActors.Count - 1
    PropertyActor = MyProperty.PropertyActors.Item(i)
Next