Array.copy [英] Array.copy
问题描述
我有一个多维数组,我想要复制,但是当我这样做时它在第二维显示0'的
。
我可以复制一个多维数组吗?
如果是这样的话我该怎么做。
谢谢
I have a multi dimensional array, I want to copy, but when I do It shows 0''s
in the second dimension.
Can I copy a multi dimensional array?
if so how do i do it.
Thanks
推荐答案
2008年7月28日星期一21:15:47 -0700,Alan Mosley< me @ workwrote:
On Mon, 28 Jul 2008 21:15:47 -0700, Alan Mosley <me@workwrote:
我有一个多维数组,我想要复制,但是当我这样做时显示
0'的第二维
。
>
我可以复制多维数组吗?
I have a multi dimensional array, I want to copy, but when I do It shows
0''s
in the second dimension.
Can I copy a multi dimensional array?
是。
Yes.
如果是这样我该怎么做。
if so how do i do it.
您需要单独复制每个子数组。在Java中,
多维数组实际上只是一个数组数组。
Pete
You need to copy each sub-array individually. In Java, a
"multi-dimensional" array is really just an array of arrays.
Pete
你好Alan,
这是一个例子:
int [,] nums = {{1,2},{3,4}};
int [,] numsCopy = new int [nums.GetUpperBound(0)+ 1,
nums.GetUpperBound(1)+ 1];
for(int i = 0; i< = nums.GetUpperBound(0); i ++)
{
for(int j = 0; j< = nums .GetUpperBound(1); j ++)
{
numsCopy [i,j] = nums [i,j];
Console.WriteLine (" numsCopy [{0},{1}]:{2}",i,j,
numsCopy [i,j]);
} < br $>
}
Joe
http://www.csharp-station.com
" Alan Mosley" < me @ workwrote in message
news:e1 ************** @ TK2MSFTNGP03.phx.gbl ...
Hi Alan,
Here''s an example:
int[,] nums = { { 1, 2 }, { 3, 4 } };
int[,] numsCopy = new int[nums.GetUpperBound(0) + 1,
nums.GetUpperBound(1) + 1];
for (int i = 0; i <= nums.GetUpperBound(0); i++)
{
for (int j = 0; j <= nums.GetUpperBound(1); j++)
{
numsCopy[i, j] = nums[i, j];
Console.WriteLine("numsCopy[{0},{1}]: {2}", i, j,
numsCopy[i, j]);
}
}
Joe
http://www.csharp-station.com
"Alan Mosley" <me@workwrote in message
news:e1**************@TK2MSFTNGP03.phx.gbl...
>我有一个多维数组,我想复制,但是当我这样做时它在第二维显示
0'。
我可以复制一个多维数组吗?
如果是这样的话我是怎么做的。
谢谢
>
2008年7月28日星期一21:39:08 -0700,Peter Duniho
< Np ******* **@nnowslpianmk.comwrote:
On Mon, 28 Jul 2008 21:39:08 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
[...]
您需要单独复制每个子阵列。在Java中,
多维 array实际上只是一个数组数组。
[...]
You need to copy each sub-array individually. In Java, a
"multi-dimensional" array is really just an array of arrays.
叹气......抱歉。在Java程序的中间,并且对于我所在的新闻组感到困惑。
无论如何,C#确实支持真正的多维"阵列。但是,它还支持锯齿状和锯齿状。数组,是数组的数组。因此,根据你创建多维数组的方式,你可能实际上不得不像我写的那样单独复制每个子数组。
如果你有一个真正的多维数组,那么你可以使用Array.Copy(),
将你的多维数组视为一个长数组(它是
真的是:))。在这种情况下,您希望传递给
Copy()方法的长度将是您要复制的所有元素的总数。
所有尺寸。例如,如果你有一个10x10数组,你会传递一个
长度100来将所有元素从一个数组复制到另一个数组。
我为这种混乱道歉。
Pete
Sigh...sorry. In the middle of a Java program, and got confused about
what newsgroup I''m in. :)
Anyway, C# does support true "multi-dimensional" arrays. However, it also
supports "jagged" arrays, which are arrays of arrays. So, depending on
how you created your multi-dimensional array, you might in fact have to
copy each sub-array individually as I wrote.
If you have a true multi-dimensional array, then you can use Array.Copy(),
which will treat your multi-dimensional array as one long array (which it
is really is :) ). In that case, the length you''ll want to pass for the
Copy() method would be the total number of elements you want copied over
all dimensions. For example, if you have a 10x10 array, you''d pass a
length of 100 to copy all elements from one array to the other.
I apologize for the confusion.
Pete
这篇关于Array.copy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!