通过HashTable迭代 [英] Iterating through a HashTable

查看:120
本文介绍了通过HashTable迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由某个名称键入的哈希表,包含一个对象的实例。大多数

的时候我使用传统意义上的哈希表,给定一个名字,我查找对象,然后我在该对象上运行一个方法。然而,偶然地,我需要在哈希表中的每个对象上运行一个方法。我已经完成了两个

For..Each和iEnumeratior循环。凌乱的部分是在某些条件下,在迭代时,我需要从哈希表中删除一个

对象。使用ArrayLists,我使用反向

迭代(从最后到第一个)处理它并执行.RemoveAt。哈希表没有

..RemoveAt。 For..Each不起作用,因为.Remove需要密钥而且我只有
有对象,iEnumerator不起作用,因为我在

之后得到一个错误。删除说我的枚举器现在无效,所以我不能继续那个

迭代。


我想过几个解决方案主要涉及单独的线程,但

想知道是否有更清洁的方法来处理这个问题。有人遇到这个?b $ b这个?这似乎是一个相当标准的问题,必须有一些模式

解决方案。

-Ken

I have a hashtable keyed by some name, holding an instance of an object. Most
of the time I use the hashtable in the traditional sense, given a name, I
lookup the object, then I run a method on that object. Occaisionally however
I need to run a method on every object in the hashtable. I''ve done both
For..Each and iEnumeratior loops. The messy part is that under certain
conditions, while iterating, a condition exists where I need to remove an
object from the hashtable. With ArrayLists I handle this with a reverse
iteration (from last to first) and do a .RemoveAt. Hashtables do not have
..RemoveAt. For..Each doesnt work because .Remove needs the key and I only
have the object, iEnumerator doesn''t work because I get an error after the
first .Remove saying my enumerator is now invalid, so I can''t continue that
iteration.

I''ve thought about several solutions mostly inlvolving separate threads, but
wanted to know if there was a cleaner way to handle this. Anyone come across
this? It seems like a fairly standard problem, there must be some "pattern"
solution.
-Ken

推荐答案

如果不太贵,请尝试复制密钥集合。


Dim h As New Hashtable

Dim key,value As Object

Dim keys As ICollection = h.Keys

For Each key In keys

value = h(key)

如果是value.condition然后

h。移除(键)

结束如果

下一页


HTH
Try making a copy of the keys collection if it isn''t too expensive.

Dim h As New Hashtable
Dim key, value As Object
Dim keys As ICollection = h.Keys
For Each key In keys
value = h(key)
If value.condition Then
h.Remove(key)
End If
Next

HTH


Uuhm ...也许实际制作副本会有所帮助。


昏暗键,值作为对象

Dim keys As ICollection = h.Keys

Dim keylist As New ArrayList

keylist.AddRange(keys)

每个键在键列表中

如果CInt(键)> 5然后

h。移动(键)

结束如果

下一页


" Jonas Pohlandt" < J。****************** @ dbit-systems.de> schrieb im

Newsbeitrag新闻:OE ************** @ tk2msftngp13.phx.gbl ...
Uuhm... maybe actually making a copy would help.

Dim key, value As Object
Dim keys As ICollection = h.Keys
Dim keylist As New ArrayList
keylist.AddRange(keys)
For Each key In keylist
If CInt(key) > 5 Then
h.Remove(key)
End If
Next

"Jonas Pohlandt" <j.******************@dbit-systems.de> schrieb im
Newsbeitrag news:OE**************@tk2msftngp13.phx.gbl...
试着复制一份密钥集合,如果它不是太贵。

Dim h As New Hashtable
Dim key,value As Object
Dim keys As ICollection = h.Keys
For Each键In键
value = h(键)
如果value.condition则
h。移动(键)
结束如果
下一页

HTH
Try making a copy of the keys collection if it isn''t too expensive.

Dim h As New Hashtable
Dim key, value As Object
Dim keys As ICollection = h.Keys
For Each key In keys
value = h(key)
If value.condition Then
h.Remove(key)
End If
Next

HTH



Ken,


我知道你不是迭代通过

hasthtable的键但是通过值。


在我的想法中,不是哈希表不在哪里,在这种情况下由

实例数据表作为集合做得更好,因为你有更多的方法从数据行中的对象获取值和

在需要时删除这些行。


但是我可能看错了吗?


Cor
Ken,

I have the idea that you are not iterating through the keys of the
hasthtable but throught the values.

In my idea is that not where the hashtable is not made for, in that case by
instance the datatable does a much better job as collection because you have
much more methods to get the values from the objects in the datarow and
remove when needed those rows.

However maybe I see something wrong?

Cor


这篇关于通过HashTable迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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