473,799 Members | 3,817 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3375
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.GetUpperBo und(0) + 1,
nums.GetUpperBo und(1) + 1];

for (int i = 0; i <= nums.GetUpperBo und(0); i++)
{
for (int j = 0; j <= nums.GetUpperBo und(1); j++)
{
numsCopy[i, j] = nums[i, j];
Console.WriteLi ne("numsCopy[{0},{1}]: {2}", i, j,
numsCopy[i, j]);
}
}

Joe
http://www.csharp-station.com

"Alan Mosley" <me@workwrote in message
news:e1******** ******@TK2MSFTN GP03.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*********@nn owslpianmk.comw rote:
[...]
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.mic rosoft.com
<jm***@csharp-stat10n.comwrot e:
Hi Alan,

Here's an example:

int[,] nums = { { 1, 2 }, { 3, 4 } };

int[,] numsCopy = new int[nums.GetUpperBo und(0) + 1,
nums.GetUpperBo und(1) + 1];

for (int i = 0; i <= nums.GetUpperBo und(0); i++)
{
for (int j = 0; j <= nums.GetUpperBo und(1); j++)
{
numsCopy[i, j] = nums[i, j];
Console.WriteLi ne("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*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Mon, 28 Jul 2008 21:39:08 -0700, Peter Duniho
<Np*********@nn owslpianmk.comw rote:
>[...]
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*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Mon, 28 Jul 2008 21:50:53 -0700, privatenews.mic rosoft.com
<jm***@csharp-stat10n.comwrot e:
>Hi Alan,

Here's an example:

int[,] nums = { { 1, 2 }, { 3, 4 } };

int[,] numsCopy = new int[nums.GetUpperBo und(0) + 1,
nums.GetUpperB ound(1) + 1];

for (int i = 0; i <= nums.GetUpperBo und(0); i++)
{
for (int j = 0; j <= nums.GetUpperBo und(1); j++)
{
numsCopy[i, j] = nums[i, j];
Console.WriteLi ne("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*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Mon, 28 Jul 2008 21:50:53 -0700, privatenews.mic rosoft.com
<jm***@csharp-stat10n.comwrot e:
>Hi Alan,

Here's an example:

int[,] nums = { { 1, 2 }, { 3, 4 } };

int[,] numsCopy = new int[nums.GetUpperBo und(0) + 1,
nums.GetUpperB ound(1) + 1];

for (int i = 0; i <= nums.GetUpperBo und(0); i++)
{
for (int j = 0; j <= nums.GetUpperBo und(1); j++)
{
numsCopy[i, j] = nums[i, j];
Console.WriteLi ne("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.WriteLi ne(iv);
}
// old way:
int[] ia2 = new int[4];
ia.CopyTo(ia2, 0);
ia2[3] = 4;
foreach(int iv in ia2)
{
Console.WriteLi ne(iv);
}
// .NET 3.5 way
Array.Resize(re f ia, 4);
ia[3] = 4;
foreach(int iv in ia2)
{
Console.WriteLi ne(iv);
}
Arne
Aug 2 '08 #10

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

Similar topics

58
10188
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 code... TCHAR myArray; DoStuff(myArray);
7
25174
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 (Bubble, insertion, selection..). I have an array of int's and am passing them to each sort function.
7
1731
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 != copy.Length; i++)
5
9850
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 the memory across. Using Marshal.PtrStructure doesn't work - it says my byte() array is not blittable! (byte is a blittable type however). Cannot use Marshal.Copy, because that works the other way around (for mashalling to COM, not from it). ...
24
3462
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 declared an array as: char *stringArray = {"one","two","three","a"}; When I pass the array using:
23
7422
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
14
20414
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 passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case the value if modified in the function will get reflected in the main function as well. I dont...
29
4257
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 a temp array and delete afterwards if necessary but since I'm actually working in a nested situation this could get a little messy. I guess I could set there values to null and remove them afterwards? Thanks, Jon
8
7446
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 realDataSent is now copy to the beginning of the sendBuffer. I have tried: Array.Copy(sendBuffer, realDataSent, sendBuffer, 0, sendBuffer.Length - realDataSent);
1
1992
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
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9543
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10237
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.