关于哈希表删除 [英] About the Hashtable Remove

查看:237
本文介绍了关于哈希表删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例代码:

Hashtable h0 = new Hashtable();
h0.add(0,10);
Hashtable h1 = new Hashtable();
h1.add(0,h0);

h1.Remove(0);

问题:

我是否需要获取h0,删除h0中的项目,然后从h1中删除h0?或者只是删除h0?

Do I need to get the h0, remove the items in h0, then remove the h0 from h1? Or just remove the h0?

推荐答案



不,您不需要拆除要从集合中删除的元素. br>
BTW:您应该考虑使用通用集合(例如Dictionary<T1,T2>),该集合从.NET 2.0开始可用;

:)

Hi,

no you don''t need to dismantle an element that you want to remove from a collection.

BTW: you should consider using generic collections (such as Dictionary<T1,T2>), available since .NET 2.0; they are faster and safer.

:)


哈希表仅包含对象的引用,而不是对象的引用.对象本身.从另一个哈希表(h1中的h0)中删除一个哈希表引用只会使 reference 到h1中的h0中. h0本身保持不变(h0中包含的所有引用也一样).
The hashtable only contains a reference to the object, not the object itself. Removing one hashtable reference from the other hastable (h0 from h1) only takes the reference to h0 out of h1. h0 itself remains unchanged (as do all references contained in h0).


我假设您的问题与垃圾回收有关.如果是这样,只需删除h0.这就是为什么...

I assume your question is about garbage collection. If so, just remove h0. Here''s why...

通过从h1中删除h0,您告诉垃圾收集器嘿,h0不再被任何东西引用,因此您可以对其进行垃圾收集".一旦h0被垃圾回收,这意味着也不会引用带框的整数(10),也使其成为垃圾回收的候选对象.

By removing h0 from h1, you tell the garbage collector "hey, h0 is no longer referenced by anything, so you can garbage collect it". Once h0 is garbage collected, that means the boxed integer (10) is also not referenced, making it a candidate for garbage collection as well.

垃圾回收可以归纳如下:如果一个对象不再被另一个对象引用,则将对其进行垃圾回收(又名从内存中释放).如果存在某种循环引用(例如,变量A引用变量B,然后引用变量A),则只要主线程可以访问的任何对象都没有引用这些对象,这些对象仍将被垃圾回收.

Garbage collection can be summarized as follows: if an object is no longer referenced by another object, it will be garbage collected (aka, released from memory). If there is some sort of circular referencing (such as variable A referencing variable B which then references variable A), then those objects will still be garbage collected so long as they aren''t referenced by anything that the main threads can access.


这篇关于关于哈希表删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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