将一个二维数组复制到另一个二维数组 [英] Copy one 2D array to another 2D array

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

问题描述

我使用此代码将一个二维数组复制到另一个二维数组:

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 的 演示.

Here is a demo on ideone.

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

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