Array.copy

A

Alan Mosley

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
 
P

privatenews.microsoft.com

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
 
T

ThatsIT.net.au

Peter Duniho said:
[...]
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
 
T

ThatsIT.net.au

Peter Duniho said:
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
 
A

Alan Mosley

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 said:
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
 
A

Arne Vajhøj

Alan said:
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
 
T

ThatsIT.net.au

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 said:
Alan said:
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
 
F

Farhan Mirza

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
const int rows = 4;
const int rowZero = 5; // num elements
const int rowOne = 2;
const int rowTwo = 3;
const int rowThree = 5;

// declare the jagged array as 4 rows high
int[][] jaggedArray = new int[rows][];

// declare the rows of various lengths
jaggedArray[0] = new int[rowZero];
jaggedArray[1] = new int[rowOne];
jaggedArray[2] = new int[rowTwo];
jaggedArray[3] = new int[rowThree];

// Fill some (but not all) elements of the rows
jaggedArray[0][3] = 15;
jaggedArray[1][1] = 12;
jaggedArray[2][1] = 9;
jaggedArray[2][2] = 99;
jaggedArray[3][0] = 10;
jaggedArray[3][1] = 11;
jaggedArray[3][2] = 12;
jaggedArray[3][3] = 13;
jaggedArray[3][4] = 14;

for (int i = 0; i < rowZero; i++)
{
Console.WriteLine("jaggedArray[0][{0}] = {1}",
i, jaggedArray[0]);
}

for (int i = 0; i < rowOne; i++)
{
Console.WriteLine("jaggedArray[1][{0}] = {1}",
i, jaggedArray[1]);
}

for (int i = 0; i < rowTwo; i++)
{
Console.WriteLine("jaggedArray[2][{0}] = {1}",
i, jaggedArray[2]);
}
for (int i = 0; i < rowThree; i++)
{
Console.WriteLine("jaggedArray[3][{0}] = {1}",
i, jaggedArray[3]);
}
Console.ReadLine();

int[][] newjaggedarray = new int[rows][];

Array.Copy(jaggedArray, newjaggedarray, jaggedArray.Length);

for (int i = 0; i < newjaggedarray[0].Length; i++)
{
Console.WriteLine("newjaggedArray[0][{0}] = {1}",
i, newjaggedarray[0]);
}

for (int i = 0; i < newjaggedarray[1].Length; i++)
{
Console.WriteLine("newjaggedArray[1][{0}] = {1}",
i, newjaggedarray[1]);
}

for (int i = 0; i < newjaggedarray[2].Length; i++)
{
Console.WriteLine("newjaggedArray[2][{0}] = {1}",
i, newjaggedarray[2]);
}
for (int i = 0; i < newjaggedarray[3].Length; i++)
{
Console.WriteLine("newjaggedArray[3][{0}] = {1}",
i, newjaggedarray[3]);
}
Console.ReadLine();
}
}
}




Alan Mosley wrote:

Array.copy
29-Jul-08

I have a multi dimensional array, I want to copy, but when I do It shows 0'
in the second dimension

Can I copy a multi dimensional array

if so how do i do it


Thanks

Previous Posts In This Thread:

Array.copy
I have a multi dimensional array, I want to copy, but when I do It shows 0'
in the second dimension

Can I copy a multi dimensional array

if so how do i do it


Thanks

Re: Array.copy
Yes

You need to copy each sub-array individually. In Java,
"multi-dimensional" array is really just an array of arrays

Pete

Re: Array.copy
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


Re: Array.copy
On Mon, 28 Jul 2008 21:39:08 -0700, Peter Duniho


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

Re: Array.copy
On Mon, 28 Jul 2008 21:50:53 -0700, privatenews.microsoft.com


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

Re: Array.copy
thanks ill try that

Re: Array.copy
thanks
I will will give it a try tomorrow

Peter I should of made myself clear, i want to inlarge the array.
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

Re: Array.copy


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

Re: Array.copy
Alan said:
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

sorry I did not get back for such a long time, I sorted the problem another
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.


Submitted via EggHeadCafe - Software Developer Portal of Choice
Measuring SharePoint Page Rendering
http://www.eggheadcafe.com/tutorial...1-9d5f236c2be5/measuring-sharepoint-page.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top