我看到了一些关于哈希表浅拷贝和深拷贝的文章/解释,读得越多,就越感到困惑。
Hashtable ht = new Hashtable();
ht.Add("1", "hello");
Hashtable ht2 = new Hashtable();
ht2 = ht; // case1: is this shallow copy?
ht2["1"] = "H2";
Hashtable ht3 = new Hashtable(ht); // case2: is this shallow copy?
ht3["1"] = "H3";
Hashtable ht4 = new Hashtable();
ht4 = (Hashtable)ht.Clone(); // case3: is this shallow copy?
ht4["1"] = "H4";
- Case1: 结果,ht内容更改为与ht2相同。
- Case2: 结果,ht内容与ht3不同。
- Case3: 结果,ht内容与ht4不同。
如果Case2和Case3是浅拷贝,那么结果不应该与Case1相同吗?
List、ArrayList等也会发生这种情况吗?