后台-插件-广告管理-内容页广告位一(手机)

您现在的位置是:首页 > 开发类 > 问答问答

ArrayList问题~~求救st

2021-07-06 20:21:17问答人已围观

简介 <P>ArrayList应该是引用类型对吧?</P>
<P>我现在想把list1赋给list2但是ArrayList是引用类,我改变list1就会改变list2</P>
<P>很郁闷,高手帮帮忙,下面是代码,我试

<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>

最佳答案:你的方法一 是引用传递 方法二和方法三都是重建立了一个对象 我把我想到的恶情况都写在这里了你去看看吧 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

文章来源:https://q.cnblogs.com/q/2594/

Tags:.net技术 c 

很赞哦! ()

上一篇:求DB2 破解版下载

下一篇:返回列表

后台-插件-广告管理-内容页广告位二(手机)

相关文章

后台-插件-广告管理-内容页广告位三(手机)
后台-插件-广告管理-内容页广告位四(手机)

文章评论

留言与评论(共有 0 条评论)
   
验证码:

本栏推荐

站点信息

  • 文章统计90166篇文章
  • 浏览统计10086次浏览
  • 评论统计1个评论
  • 标签管理标签云
  • 统计数据:统计代码
  • 微信公众号:扫描二维码,关注我们