关于Clone的问题。

bruse82 2004-10-23 05:29:58
请教如何对一个Arraylist进行clone,要求是对Arraylist里的对象也值进行值的复制而不是它的地址?
多谢。
...全文
174 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eddie005 2004-10-23
  • 打赏
  • 举报
回复
但是Clone是浅副本
Eddie005 2004-10-23
  • 打赏
  • 举报
回复
sorry,搞错了
Dim tmp As System.Collections.ArrayList = New System.Collections.ArrayList
Dim tmp2 As System.Collections.ArrayList = tmp.Clone()
BearRui 2004-10-23
  • 打赏
  • 举报
回复
ArrayList有Clone 方法,不过好象不会复制数据!!!
BearRui 2004-10-23
  • 打赏
  • 举报
回复
see:

using System;
using System.Collections;
public class SamplesArrayList {

public static void Main() {

// Creates and initializes the source ArrayList.
ArrayList mySourceList = new ArrayList();
mySourceList.Add( "three" );
mySourceList.Add( "napping" );
mySourceList.Add( "cats" );
mySourceList.Add( "in" );
mySourceList.Add( "the" );
mySourceList.Add( "barn" );

// Creates and initializes the one-dimensional target Array.
Array myTargetArray=Array.CreateInstance( typeof(String), 15 );
myTargetArray.SetValue( "The", 0 );
myTargetArray.SetValue( "quick", 1 );
myTargetArray.SetValue( "brown", 2 );
myTargetArray.SetValue( "fox", 3 );
myTargetArray.SetValue( "jumped", 4 );
myTargetArray.SetValue( "over", 5 );
myTargetArray.SetValue( "the", 6 );
myTargetArray.SetValue( "lazy", 7 );
myTargetArray.SetValue( "dog", 8 );

// Displays the values of the target Array.
Console.WriteLine( "The target Array contains the following (before and after copying):" );
PrintValues( myTargetArray, ' ' );

// Copies the second element from the source ArrayList to the target ArrayList, starting at index 7.
mySourceList.CopyTo( 1, myTargetArray, 7, 1 );

// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );

// Copies the entire source ArrayList to the target ArrayList, starting at index 6.
mySourceList.CopyTo( myTargetArray, 6 );

// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );

// Copies the entire source ArrayList to the target ArrayList, starting at index 0.
mySourceList.CopyTo( myTargetArray );

// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
}

public static void PrintValues( Array myArr, char mySeparator ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
}
Console.WriteLine();
}
}
Eddie005 2004-10-23
  • 打赏
  • 举报
回复
Arraylistm没有Clone方法,只有CopyTo方法

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧