Hi,
I am a newbie in C# and I am having problems initializing multi-dimensional jagged arrays.
I want to create a two dimensional array of Points. I am using arrays bcoz I dont know any other easier collection but if anybody knows a better and easy collection please let me know.
I am declaring my array like this: Point[][] gPoints;
In the first dimension I want to store 4 points and each of those dimensions containing 4 points will have jagged array of points. So first I want to initialize the first dimension with 4 points and then later add points dynamically to each dimensions.
My program divides the canvas into small squares of fixed dimensions and for each of those squares there will be different no. of Points..Below is the initialization which is absolutely incorrect: Please someone correct it...
for
(int x = 0; x < Dimension; x++){gPoints[x] =
new Point[Dimension][] {new Point[]{{ new Point( x, x )}, {new Point ( x, y )}, {new Point (y, y )}, {new Point ( y, x ) }}};}
AlanPosted Sep 12, 2007, 7:35 PM
It looks to me as though you're going to need a four dimensional array to deal with all those combinations:
int canvasDim = Convert.ToInt32(canvasDimension);
Point[][][][] gPoints = new Point[canvasDim][][][];
for (int z = 0; z < canvasDim; z++)
{
gPoints[z] = new Point[canvasHeight][][];
for (int y = 0; y < canvasHeight; y += 10)
{
gPoints[z][y] = new Point[canvasWidth][];
for (int x = 0; x < canvasWidth; x += 10)
{
gPoints[z][y][x] = new Point[4] {new Point(x, x), new Point(x + 10, x), new Point(x + 10, y + 10), new Point(x, y + 10)};
}
}
}
In fact, if the first three dimensions will remain fixed in size and it's only the fourth dimension that will be jagged, you could simplify matters a bit by using a three dimensional array of jagged arrays:
Point[,,][] hPoints = new Point[canvasDim, canvasHeight, canvasWidth][];
for (int z = 0; z < canvasDim; z++)
{
for (int y = 0; y < canvasHeight; y += 10)
{
for (int x = 0; x < canvasWidth; x += 10)
{
hPoints[z,y,x] = new Point[4] {new Point(x, x), new Point(x + 10, x), new Point(x + 10, y + 10), new Point(x, y + 10)};
}
}
}
}
Spark CPosted Sep 12, 2007, 6:26 PM
Thanks so much..That worked but I guess I will need 3 dimensional arrays bcoz for each of those four points I need one dynamic jagged array. So how will I achieve that..At present my code looks like this:
double
canvasDimension = (canvasWidth/10) * (canvasHeight/10); Point[][] gPoints = new Point[Convert.ToInt32(canvasDimension)][]; int z=0; for (int y = 0; y < canvasHeight; y += 10){
for (int x = 0; x < canvasWidth; x+=10){
gPoints[z] = new Point[4] { new Point(x, y), new Point(x + 10, y), new Point(x + 10, y + 10), new Point(x, y + 10) };z++;
}
}
I want to add one more array for each gPoints..gPoints[0]..gPoints[1]..
AlanPosted Sep 12, 2007, 5:54 PM
This syntax should work:
Point[][] gPoints = new Point[Dimension][];
for (int x = 0; x < Dimension; x++)
{
gPoints[x] = new Point[4]{new Point(x,x), new Point(x,y), new Point(y,y),new Point(y,x)};
}
Notice though that the only way you'll be able to add new points in the future to a given gPoints[x] for some value of x is to create a new single dimensional array of Point and then assign that to gPoints[x], thereby replacing the original array.
So, I'd either wait until you do know how many points you'll need before initializing gPoints[x] or, if you can't say for sure how many points will be added, then I'd use a single dimensional array of List if you're using .NET 2.0 or later.