数组没有itemList copyList=new List();foreach(object a in A){copyList.Add(a);}B=copyList.ToArray();//需要引用System.Linq;return B;上面这样做,不需要知道A数组的长度。 ==========================如果你是值类型或string数组很简单: #region IntV数组复制 /// /// 方法一:使用for循环 /// /// /// public IntV[] CopyObjectArray1(IntV[] pins) { //IntV []pins = {9,3,7,2} IntV[] copy = new IntV[pins.Length]; for (int i = 0; i < pins.Length; i++) { copy[i] = pins[i]; } return copy; }/// /// 方法二:使用数组对象中的CopyTo()方法 /// /// /// public IntV[] CopyObjectArray2(IntV[] pins) { //IntV []pins = {9,3,7,2} IntV[] copy2 = new IntV[pins.Length]; pins.CopyTo(copy2, 0); return copy2; } /// /// 方法三:使用Array类的一个静态方法Copy() /// /// /// public IntV[] CopyObjectArray3(IntV[] pins) { //IntV []pins = {9,3,7,2} IntV[] copy3 = new IntV[pins.Length]; Array.Copy(pins, copy3, pins.Length); return copy3; } /// /// 方法四:使用Array类中的一个实例方法Clone(),可以一次调用,最方便,但是Clone()方法返回的是一个对象,所以要强制转换成恰当的类类型。 /// /// /// public IntV[] CopyObjectArray4(IntV[] pins) { //IntV []pins = {9,3,7,2} IntV[] copy4 = (IntV[])pins.Clone(); return copy4; } #endregion如果是对象数组深度拷贝,则要用其它方法了:这个是对象必须继承序列化[Serializable] public static object CloneObj(object obj) { BinaryFormatter Formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone)); MemoryStream stream = new MemoryStream(); Formatter.Serialize(stream, obj); stream.Position = 0; object clonedObj = Formatter.Deserialize(stream); stream.Close(); return clonedObj; }另外一种深度复制,就是继承接口ICloneable public class IntV : ICloneable { public int V { get; set; } public override string ToString() { return V.ToString(); } public object Clone() { return MemberwiseClone() ; } }下面你只要这样使用就可以了 IntV v1 = new IntV();v1.V = 10; IntV v2 = v1.Clone() as IntV; v2.V = 99;
大哥,你这样是传递的引用,不是复制!!!!!a = (object[])b.Clone();其实只要这样就可以了
for(int i=0;iobjectA[i]=objictB[i];n为数组大小