首页 新闻 搜索 专区 学院

ArrayList问题~~求救st

0
[已解决问题] 解决于 2008-10-12 14:49
<P>ArrayList应该是引用类型对吧?</P> <P>我现在想把list1赋给list2但是ArrayList是引用类,我改变list1就会改变list2</P> <P>很郁闷,高手帮帮忙,下面是代码,我试了3种方法,还是不行</P> <DIV class=cnblogs_code><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><SPAN style="COLOR: #008080">&nbsp;1</SPAN><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">ArrayList&nbsp;list1&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;ArrayList();<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>ArrayList&nbsp;list2&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;ArrayList();<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN><SPAN style="COLOR: #000000"><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN><SPAN style="COLOR: #000000"><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top>list2</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">list1;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">方法一,行不同</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN><SPAN style="COLOR: #008000"><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">list2</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">(ArrayList)list1.clone();&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">方法二,也行不通</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN><SPAN style="COLOR: #008000"><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000">list2</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;ArrayList(list1);&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">方法三,还是行不通</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN><SPAN style="COLOR: #008000"><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_184_212_Open_Image onclick="this.style.display='none'; document.getElementById('Codehighlighter1_184_212_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_184_212_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_184_212_Closed_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_184_212_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_184_212_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_184_212_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_184_212_Open_Text').style.display='inline';" src="http://space.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_184_212_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_184_212_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN><SPAN style="COLOR: #008000"><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN><SPAN style="COLOR: #008000"><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*&nbsp;高手帮帮忙谢谢~~~~~<BR></SPAN><SPAN style="COLOR: #008080">11</SPAN><SPAN style="COLOR: #008000"><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;*<BR></SPAN><SPAN style="COLOR: #008080">12</SPAN><SPAN style="COLOR: #008000"><IMG src="http://space.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN></DIV>
Richard Law的主页 Richard Law | 初学一级 | 园豆:0
提问于:2008-08-27 03:35
< >
分享
最佳答案
0
你的方法一 是引用传递 方法二和方法三都是重建立了一个对象 我把我想到的恶情况都写在这里了你去看看吧 ArrayList arrayList1 = new ArrayList(); arrayList1.Add("1"); ArrayList arrayList2 = arrayList1; Response.Write("arrayList1:" + arrayList1[0].ToString() + "<br/>"); Response.Write("arrayList2:"+ arrayList2[0].ToString() + "<br/>"); arrayList2[0] = "2"; Response.Write("arrayList1:" + arrayList1[0].ToString() + "<br/>"); Response.Write("arrayList2:" + arrayList2[0].ToString() + "<br/>"); arrayList2[0] = arrayList1[0]; arrayList1[0] = "3"; Response.Write("arrayList1:" + arrayList1[0].ToString() + "<br/>"); Response.Write("arrayList2:" + arrayList2[0].ToString() + "<br/>"); arrayList2 = new ArrayList(); arrayList2.Add(arrayList1[0]); arrayList1[0] = "4"; Response.Write("arrayList1:" + arrayList1[0].ToString() + "<br/>"); Response.Write("arrayList2:" + arrayList2[0].ToString() + "<br/>"); arrayList2 = arrayList1; arrayList1[0] = "5"; Response.Write("arrayList1:" + arrayList1[0].ToString() + "<br/>"); Response.Write("arrayList2:" + arrayList2[0].ToString() + "<br/>"); arrayList2 = (ArrayList)arrayList1.Clone(); arrayList1[0] = "6"; Response.Write("arrayList1:" + arrayList1[0].ToString() + "<br/>"); Response.Write("arrayList2:" + arrayList2[0].ToString() + "<br/>"); arrayList2 = new ArrayList(arrayList1); arrayL
小眼睛老鼠 | 老鸟四级 |园豆:2774 | 2008-08-27 14:00
其他回答(6)
0
foreach(object obj in list1) { list2.add(obj); }
业余爱好者 | 园豆:255 (菜鸟二级) | 2008-08-27 08:35
0
请实现深拷贝,可以使用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的支持
lexus | 园豆:0 (初学一级) | 2008-08-27 10:00
0
用foreach把list1赋给list2就可以了。
金鱼 | 园豆:1090 (小虾三级) | 2008-08-27 15:12
0
foreach
翅膀 | 园豆:335 (菜鸟二级) | 2008-09-01 12:10
0

  问题的症结是 IClone(?) 这个接口定义的不严谨,有人把它实现为深拷贝,有人把实现为潜拷贝。因此有专家建议尽量避免使用该接口,遇到类似需要,自己根据需要,自己动手。说实话,即便你按如上说的用 foreach “深拷贝”了可能还是不行。有没有想过 foreach(object obj in list1) { list2.add(obj); } 这里的 obj 依旧可能是引用类型,当我们 list1[0].something = 'something' 时, list2[0].something 也跟着改了。只不过避免了你进行 list1.Insert、list1.Remove等操作给 list2 带来影响而已。

  看你的实际需求了。如果你只是想两个数组进行隔离,大概 foreach 一下就可以了;如果是想这两个数组以及元素之间绝对隔离,估计这个就很难办了,至少要费很大劲。

陛下 | 园豆:3938 (老鸟四级) | 2008-09-07 04:15
0

这里使用了ArryList 的Clone()方法

可以啊,一切正常

代码:   经过测试,大家可以试下 我更改了list2里的一个值,list里相对会位置的值保持了原值没有变化

 

Code
protected void Button6_Click(object sender, EventArgs e)
{
int length = 10;
System.Collections.ArrayList list
= new System.Collections.ArrayList();
for (int i = 0; i < length; i++)
{
list.Add(i.ToString());
}

System.Collections.ArrayList list2
= new System.Collections.ArrayList();
list2
= (System.Collections.ArrayList)list.Clone();

list2[
5] = "新加的内容";

for (int i = 0; i < list.Count; i++)
{
Response.Write(list[i].ToString());
}
Response.Write(
"</br>");
for (int i = 0; i < list2.Count; i++)
{
Response.Write(list2[i].ToString());
}
}
一杰 | 园豆:790 (小虾三级) | 2008-09-10 09:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册