将一个2D阵列复制到另一个2D阵列 [英] Copy one 2D array to another 2D array

查看:93
本文介绍了将一个2D阵列复制到另一个2D阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码将一个2D数组复制到另一个2D数组:

I used this code to copy one 2D array to another 2D array:

Array.Copy(teamPerformance, 0,tempPerformance,0, teamPerformance.Length);

但是,当我更改tempPerformance中的某些数据时,这些更改也适用于teamPerformance.
我该怎么做才能控制它?

However, when I change some data in tempPerformance then these changes also apply to teamPerformance.
What should I do to control that?

推荐答案

这是正确的:Array.Copy执行 shallow 复制,因此内部维内的数组实例将通过引用复制.您可以使用LINQ进行复制,如下所示:

This is correct: Array.Copy performs a shallow copy, so the instances of arrays inside the inner dimension get copied by reference. You can use LINQ to make a copy, like this:

var copy2d = orig2d.Select(a => a.ToArray()).ToArray();

这是关于ideone的演示.

这篇关于将一个2D阵列复制到另一个2D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆