473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Array.copy

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
Jul 29 '08 #1
10 3338
On Mon, 28 Jul 2008 21:15:47 -0700, Alan Mosley <me@workwrote:
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.
You need to copy each sub-array individually. In Java, a
"multi-dimensional" array is really just an array of arrays.

Pete
Jul 29 '08 #2
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...
>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

Jul 29 '08 #3
On Mon, 28 Jul 2008 21:39:08 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
[...]
You need to copy each sub-array individually. In Java, a
"multi-dimensional" array is really just an array of arrays.
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
Jul 29 '08 #4
On Mon, 28 Jul 2008 21:50:53 -0700, privatenews.microsoft.com
<jm***@csharp-stat10n.comwrote:
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]);
}
}
Actually, the above can be shorted to:

int[,] numsCopy = new int[nums.GetLength(0), nums.GetLength(1)];

Array.Copy(nums, numsCopy, nums.GetLength(0) * nums.GetLength(1));

Note also the use of GetLength() instead of GetUpperBound(). This not
only accounts for situations where the lower bound isn't 0, but also
avoids the need to adjust the upper bound by 1 even when the lower bound
is 0. :)

Pete
Jul 29 '08 #5

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 28 Jul 2008 21:39:08 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
>[...]
You need to copy each sub-array individually. In Java, a
"multi-dimensional" array is really just an array of arrays.

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
thanks ill try that

Jul 30 '08 #6

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 28 Jul 2008 21:50:53 -0700, privatenews.microsoft.com
<jm***@csharp-stat10n.comwrote:
>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]);
}
}

Actually, the above can be shorted to:

int[,] numsCopy = new int[nums.GetLength(0), nums.GetLength(1)];

Array.Copy(nums, numsCopy, nums.GetLength(0) * nums.GetLength(1));

Note also the use of GetLength() instead of GetUpperBound(). This not
only accounts for situations where the lower bound isn't 0, but also
avoids the need to adjust the upper bound by 1 even when the lower bound
is 0. :)

Pete
thanks
I'll will give it a try tomorrow

Jul 30 '08 #7
Peter I should of made myself clear, i want to inlarge the array.

Im trying to do as i would in VB using redim preserve
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 28 Jul 2008 21:50:53 -0700, privatenews.microsoft.com
<jm***@csharp-stat10n.comwrote:
>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]);
}
}

Actually, the above can be shorted to:

int[,] numsCopy = new int[nums.GetLength(0), nums.GetLength(1)];

Array.Copy(nums, numsCopy, nums.GetLength(0) * nums.GetLength(1));

Note also the use of GetLength() instead of GetUpperBound(). This not
only accounts for situations where the lower bound isn't 0, but also
avoids the need to adjust the upper bound by 1 even when the lower bound
is 0. :)

Pete

Jul 31 '08 #8
On Wed, 30 Jul 2008 19:10:12 -0700, Alan Mosley <me@workwrote:
Peter I should of made myself clear, i want to inlarge the array.

Im trying to do as i would in VB using redim preserve
I don't understand your follow-up at all. It's completely different from
the original question.

That said, C# won't allow you to reallocate an array in-place. You have
to create a new one and copy the elements.

Depending on which dimension(s) you are changing, you might still be able
to get away with using the Array.Copy() method. But I wouldn't bother
unless you really had a serious performance issue that needed to be
addressed. Otherwise, just copy each element explicitly. If you fix
Joe's code so that it's using the correct lower and upper bound as
appropriate to your actual arrays, then it'd be as simple as code like his
and then assigning the temporary destination array reference back to your
original when you're done.

Pete
Jul 31 '08 #9
Alan Mosley wrote:
Peter I should of made myself clear, i want to inlarge the array.

Im trying to do as i would in VB using redim preserve
In .NET 3.5 you have the Resize. In older version you have
top new and Copy. Note that Resize also copies - it just hides
the fact from you.

Code snippet:

int[] ia = { 1, 2, 3 };
foreach(int iv in ia)
{
Console.WriteLine(iv);
}
// old way:
int[] ia2 = new int[4];
ia.CopyTo(ia2, 0);
ia2[3] = 4;
foreach(int iv in ia2)
{
Console.WriteLine(iv);
}
// .NET 3.5 way
Array.Resize(ref ia, 4);
ia[3] = 4;
foreach(int iv in ia2)
{
Console.WriteLine(iv);
}
Arne
Aug 2 '08 #10
sorry I did not get back for such a long time, I sorted the problem another
way, but still would like to know a better way.
I seen your example but I was trying to resize a multidimensional array.

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
Alan Mosley wrote:
Peter I should of made myself clear, i want to inlarge the array.

Im trying to do as i would in VB using redim preserve

In .NET 3.5 you have the Resize. In older version you have
top new and Copy. Note that Resize also copies - it just hides
the fact from you.

Code snippet:

int[] ia = { 1, 2, 3 };
foreach(int iv in ia)
{
Console.WriteLine(iv);
}
// old way:
int[] ia2 = new int[4];
ia.CopyTo(ia2, 0);
ia2[3] = 4;
foreach(int iv in ia2)
{
Console.WriteLine(iv);
}
// .NET 3.5 way
Array.Resize(ref ia, 4);
ia[3] = 4;
foreach(int iv in ia2)
{
Console.WriteLine(iv);
}
Arne
Aug 18 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
7
by: Richard Forester | last post by:
Hello. I need some help understanding what goes on when an array is copied. I create 2 arrays and copy one to the other: int pins = {9, 3, 7, 2}; int copy = new int; for (int i = 0; i !=...
5
by: Robin Tucker | last post by:
I need to marshal an IntPtr (which I've got from GlobalLock of an HGLOBAL) into a byte array. I know the size of the array required and I've got a pointer to the blob, but I can't see how to copy...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
8
by: linuxfedora | last post by:
Which one is faster or any other better way to do it. I have an array of byte with name: sendBuffer, and i will like to make some thing like that the value started from index of the array in...
1
rahulephp
by: rahulephp | last post by:
I want to print array something like this level1 level2 Who we are . Welcome from jay(video) . mission statement (Copy) . vision(Copy) . what we believe(copy) . FAQ (copy)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.