ArrayList应该是引用类型对吧?
我现在想把list1赋给list2但是ArrayList是引用类,我改变list1就会改变list2
很郁闷,高手帮帮忙,下面是代码,我试了3种方法,还是不行
1ArrayList list1 = new ArrayList(); 2ArrayList list2 = new ArrayList(); 3 4list2=list1; //方法一,行不同 5list2=(ArrayList)list1.clone(); //方法二,也行不通 6list2=new ArrayList(list1); //方法三,还是行不通 7 8/**//* 9 *10 * 高手帮帮忙谢谢~~~~~11 *12 */
7 回答
幕布斯6054654
TA贡献1876条经验 获得超7个赞
你的方法一
是引用传递
方法二和方法三都是重建立了一个对象
我把我想到的恶情况都写在这里了你去看看吧
ArrayList arrayList1 = new ArrayList();
arrayList1.Add("1");
ArrayList arrayList2 = arrayList1;
Response.Write("arrayList1:" + arrayList1[0].ToString() + "
"); Response.Write("arrayList2:"+ arrayList2[0].ToString() + "
"); arrayList2[0] = "2"; Response.Write("arrayList1:" + arrayList1[0].ToString() + "
"); Response.Write("arrayList2:" + arrayList2[0].ToString() + "
"); arrayList2[0] = arrayList1[0]; arrayList1[0] = "3"; Response.Write("arrayList1:" + arrayList1[0].ToString() + "
"); Response.Write("arrayList2:" + arrayList2[0].ToString() + "
"); arrayList2 = new ArrayList(); arrayList2.Add(arrayList1[0]); arrayList1[0] = "4"; Response.Write("arrayList1:" + arrayList1[0].ToString() + "
"); Response.Write("arrayList2:" + arrayList2[0].ToString() + "
"); arrayList2 = arrayList1; arrayList1[0] = "5"; Response.Write("arrayList1:" + arrayList1[0].ToString() + "
"); Response.Write("arrayList2:" + arrayList2[0].ToString() + "
"); arrayList2 = (ArrayList)arrayList1.Clone(); arrayList1[0] = "6"; Response.Write("arrayList1:" + arrayList1[0].ToString() + "
"); Response.Write("arrayList2:" + arrayList2[0].ToString() + "
"); arrayList2 = new ArrayList(arrayList1); arrayL
"); Response.Write("arrayList2:"+ arrayList2[0].ToString() + "
"); arrayList2[0] = "2"; Response.Write("arrayList1:" + arrayList1[0].ToString() + "
"); Response.Write("arrayList2:" + arrayList2[0].ToString() + "
"); arrayList2[0] = arrayList1[0]; arrayList1[0] = "3"; Response.Write("arrayList1:" + arrayList1[0].ToString() + "
"); Response.Write("arrayList2:" + arrayList2[0].ToString() + "
"); arrayList2 = new ArrayList(); arrayList2.Add(arrayList1[0]); arrayList1[0] = "4"; Response.Write("arrayList1:" + arrayList1[0].ToString() + "
"); Response.Write("arrayList2:" + arrayList2[0].ToString() + "
"); arrayList2 = arrayList1; arrayList1[0] = "5"; Response.Write("arrayList1:" + arrayList1[0].ToString() + "
"); Response.Write("arrayList2:" + arrayList2[0].ToString() + "
"); arrayList2 = (ArrayList)arrayList1.Clone(); arrayList1[0] = "6"; Response.Write("arrayList1:" + arrayList1[0].ToString() + "
"); Response.Write("arrayList2:" + arrayList2[0].ToString() + "
"); arrayList2 = new ArrayList(arrayList1); arrayL
偶然的你
TA贡献1841条经验 获得超3个赞
请实现深拷贝,可以使用BinarySeralize
public static IDictionary DeepCopy(this IDictionary ht)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, ht);
ms.Position = 0;
IDictionary _ht = (IDictionary)bf.Deserialize(ms, null);
ms.Close();
return _ht;
}
参考这个,将类似改为ArrayList,另外这个是扩展方法,需要3.5的支持
largeQ
TA贡献2039条经验 获得超7个赞
问题的症结是 IClone(?) 这个接口定义的不严谨,有人把它实现为深拷贝,有人把实现为潜拷贝。因此有专家建议尽量避免使用该接口,遇到类似需要,自己根据需要,自己动手。说实话,即便你按如上说的用 foreach “深拷贝”了可能还是不行。有没有想过 foreach(object obj in list1)
{
list2.add(obj);
} 这里的 obj 依旧可能是引用类型,当我们 list1[0].something = 'something' 时, list2[0].something 也跟着改了。只不过避免了你进行 list1.Insert、list1.Remove等操作给 list2 带来影响而已。
看你的实际需求了。如果你只是想两个数组进行隔离,大概 foreach 一下就可以了;如果是想这两个数组以及元素之间绝对隔离,估计这个就很难办了,至少要费很大劲。
- 7 回答
- 0 关注
- 509 浏览
添加回答
举报
0/150
提交
取消