mohasad

hi,
i am facing teh following problem in C#.
i have initilaised the Jagged Array in the class constructor . i am storing the values index wise ..
but when the index is incremented the values in the previous index has been removed ..

could anyone help me in resolving the above issue ..
thanks
sadiq.



Re: Visual C# General regarding ArrayCopy

TilakGopi

Hi,

 i feel, problem is not clear.Anyways, just check the following sample(Excerpt -f rom Orielly Programming C# 1st edition)

------------------------------------------------------------------------------------

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

const int rows = 4;

// declare the jagged array as 4 rows high

int[][] jaggedArray = new int[rows][];

// the first row has 5 elements

jaggedArray[0] = new int[5];

// a row with 2 elements

jaggedArray[1] = new int[2];

// a row with 3 elements

jaggedArray[2] = new int[3];

// the last row has 5 elements

jaggedArray[3] = new int[5];

// 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 index = 0; index < 5; index++)

{

Console.WriteLine("jaggedArray[0][{0}] = {1}",

index, jaggedArray[0][index]);

}

for (int index = 0; index < 2; index++)

{

Console.WriteLine("jaggedArray[1][{0}] = {1}",

index, jaggedArray[1][index]);

}

for (int index = 0; index < 3; index++)

{

Console.WriteLine("jaggedArray[2][{0}] = {1}",

index, jaggedArray[2][index]);

}

for (int index = 0; index < 5; index++)

{

Console.WriteLine("jaggedArray[3][{0}] = {1}",

index, jaggedArray[3][index]);

}

}

}

}

------------------------------------------------------------------

Hope this'll clear ur problem.If not , please specify ur problm bit clear.Like what dimensions ur using(if u post the code snippet , it'll be more good enough).

 

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C# General regarding ArrayCopy

mohasad

hi tilak ,
thanks. my problem is as follows
for (int i = 0; i < 5; i++)
{
Array.Copy(load, 0, PuzzlesIdea, i * Length, Length);
}
load is a single dimension array having a dimension of 4500. Length = 900 ..
where as Puzzles is as follows:
int[][] Puzzles = new int [5][];
for(int i = 0; i < 5; i++)
{
PuzzlesIdea = new byte[Length];
}

its showing ArguementoutofArray index ......
could u resolve the above issue .
thanks
sadiq






Re: Visual C# General regarding ArrayCopy

TilakGopi

Hi,

i have got few doubts in ur code:

1)Puzzles is of int type and u r allocating byte type. Is this valid

2)why r u doing so

3)I'm using VS2005 , i haven't got the error u reported.

Try to int[][] Puzzles = new int [5][];
for(int i = 0; i < 5; i++)
{
Puzzles = new byte[Length];
}

as

int[][] Puzzles = new int [5][];
for(int i = 0; i < 5; i++)
{
Puzzles = new int[Length];
}

ur prolbem'll be solved.

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C# General regarding ArrayCopy

mohasad

hi gopy,
sorry yaar , Puzzle is a byte array .
thanks
sadiq





Re: Visual C# General regarding ArrayCopy

CyberDNA

Hi Gopi,

Can u check again your code for

int[][] Puzzles = new int [5][];
for(int i = 0; i < 5; i++)
{
Puzzles = new int[Length];
}

I think that code can't be compiled. Because as I know... Puzzles declared with int[][].

But... if you mean Puzzles is PuzzlesIdea that would be OK.

Please confirm.






Re: Visual C# General regarding ArrayCopy

TilakGopi

Hi,

 Ur right.It's PuzzlesIdea only,  i just copied from sadiq's post and pasted.Anyways thanx for pointing out.

 

Best Regards,

Ch.T.Gopi Kumar.






Re: Visual C# General regarding ArrayCopy

CyberDNA

Hi Mohasad,

from your code:

for (int i = 0; i < 5; i++)
{
Array.Copy(load, 0, Puzzles[ i ], i * Length, Length);
}

that would be some mistake, if Length = 900 and i increased and greater that 1 (one) it will throw an Exception. Coz (example I = 2) 2 * 900 will be 1800. that was out of destination start Index, coz the fact is Puzzles length just 900.

Please Clarify. Thanks






Re: Visual C# General regarding ArrayCopy

TilakGopi

Here is the code u r looking for.., it seem's u r bit confused in understanding the Array.Copy method.

----------------------------------------------

int Length = 900;

byte[][] Puzzles = new byte [5][];

for(int i = 0; i < 5; i++)

{

PuzzlesIdea = new byte[Length];

}

byte[] load = new byte[4500];

for (int i = 0; i < 5; i++)

{

Array.Copy(PuzzlesIdea, 0,load , i * Length, Length);

}

------------------------------------------

Hope u r done.

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C# General regarding ArrayCopy

mohasad

Hi,
yes , i have changed the i * Length to 0, still its showing ArguementOutrOfRange Exception .
could u point me where is the problem . and how to resolve it .
moreover load is a single dimension byte array of size 4500.
thanks
sadiq





Re: Visual C# General regarding ArrayCopy

TilakGopi

Hi , Sadiq,

Haven't u seen my reply -(reposting again).

Here is the code u r looking for.., it seem's u r bit confused in understanding the Array.Copy method.

----------------------------------------------

int Length = 900;

byte[][] Puzzles = new byte [5][];

for(int i = 0; i < 5; i++)

{

Puzzles = new byte[Length];

}

byte[] load = new byte[4500];

for (int i = 0; i < 5; i++)

{

Array.Copy(Puzzles, 0,load , i * Length, Length);

}

------------------------------------------

Hope u r done.

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C# General regarding ArrayCopy

CyberDNA

Hi mohasad,

Can u arrange you code with this code.

// Please add this code, so the array can be analyze
System.Diagnostics.Debug.WriteLine("=".PadLeft(40,'='));
System.Diagnostics.Debug.WriteLine(
string.Format(
"Puzzles.Rank : {0}",
Puzzles.Rank));
System.Diagnostics.Debug.WriteLine(
string.Format(
"Puzzles.Length : {0}",
Puzzles.Length));
System.Diagnostics.Debug.WriteLine(
string.Format(
"Puzzles.GetLowerBound(0) : {0}",
Puzzles.GetLowerBound(0)));
System.Diagnostics.Debug.WriteLine(
string.Format(
"Puzzles.GetUpperBound(0) : {0}",
Puzzles.GetUpperBound(0)));
System.Diagnostics.Debug.WriteLine("=".PadLeft(40,'='));
// ==============================================
// Your code when copying the array
for (int i = 0; i < 5; i++)
{
// Please add this block of code, so the array can be analyze
System.Diagnostics.Debug.WriteLine(
string.Format(
"Puzzles[{0}].Length : {1}",
i,
Puzzles[ i ].Length));
System.Diagnostics.Debug.WriteLine(
string.Format(
"Puzzles[{0}].GetLowerBound(0) : {1}",
i,
Puzzles[ i ].GetLowerBound(0)));
System.Diagnostics.Debug.WriteLine(
string.Format(
"Puzzles[{0}].GetUpperBound(0) : {1}",
i,
Puzzles[ i ].GetUpperBound(0)));
// ==============================================
// Your code to copy the array
Array.Copy(load, 0, Puzzles[ i ], i * Length, Length);
}

And can u put the debug result in the forum so we can analyze.

Thanks and Best Regards,






Re: Visual C# General regarding ArrayCopy

mohasad

hi tilak ,
i want to copy the data from load to puzzle .. wheather for this the above code will work .
thanks
sadiq





Re: Visual C# General regarding ArrayCopy

TilakGopi

mohasad


Posts 32
Re: regarding ArrayCopy
Was this post helpful

hi tilak ,
 i want to copy the data from load to puzzle .. wheather for this the above code will work .
thanks
sadiq



i am cool

 

 

 

 

 

 

Oh, then just interchange the load and puzzlesIdea and change indices like

------------------------------------

int Length = 900;

byte[][] Puzzles = new byte [5][];

for(int i = 0; i < 5; i++)

{

Puzzles = new byte[Length];

}

byte[] load = new byte[4500];

for (int i = 0; i < 5; i++)

{

Array.Copy(load, i * Length,PuzzlesIdea ,0 , Length);

}

--------------------------------------------------------

Hope this will work ,sadiq.

 

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C# General regarding ArrayCopy

mohasad

hi Gopi,
thanks ,
sadiq