I have a big jagged array like A[,][] with some specified dimension= ( 76*150)*(140).
How to add some elements (say x-elements) with zero values, to the begining and to the end of the bold part of that,like:
array seize before= (76*150)*140 ---> array size after adding equali-valued zero elements to begining and to the end of array= (76*150)*(150)
0 0 0 0 0 x x x x x x x x x 0 0 0 0 0
I tried to create a bigger array and copy the element of my array ,A, to that. But there is a memory problem.
how can I use ArrayList and Insert method to do this ?
thanks
yamidPosted Oct 27, 2008, 5:22 AM
thanks alot
You are really good at C#
AlanPosted Oct 26, 2008, 7:12 AM
Unless you have a lot of other very large objects in your application, I can't see why you should have a memory problem here as (assuming the jagged array is of type int), you're only going to need at most 76 x 150 x 140 x 4 = 6,384,000 bytes of memory to store it.
Using an ArrayList or List wouldn't help as these collections use an 'ordinary' array of the appropriate size as their backup store.
I'd stick to the approach you've been using by copying the 'jagged' portion to a new and bigger array and then assigning it back. The code should be something like this:
for (int i = 0; i < 76; i++)
{
for(int j = 0; j < 150; j++)
{
int originalLength = A[i,j].Length; // max 140
// all elements of Bij zero by default
int[] Bij = new int[originalLength + 10]; // max 150
// copy previous elements to Bij
Array.Copy(A[i,j], 0, Bij, 5, originalLength);
// set A[i,j] to Bij
A[i,j] = Bij;
}
}