Quote:In my case i created a 2d matrix using an 2d array.
Code:balls[x][y]
I don't believe you exactly... How did you create that array?
Maybe something like this: (warning: untested code)
Code:int numX = 30;
int numY = 20;
Ball balls1d[] = new Ball[numY * numX];
Ball balls2d[][] = new Ball[numY][numX];
int i = 0; // 1D index
for (int y = 0; y < numY; y++)
{
for (int x = 0; x < numX; x++)
{
// Create ONE ball object with TWO references
balls2d[y][x] = balls1d[i] = new Ball();
i++; // the 1d index for the next ball
}
}
Remember: the arrays only hold REFERENCES to the ball objects, and you can have as many references as you like to each object.
So, the above code (or some working version based on it) should allow you to access in two ways:
Code:int x = 3; // column 4
int y = 5; // row 6
Ball theBall = balls2d[y][x];
Ball theSameBall = balls1d[y * numX + x]; // 5 * 30 + 3 = 153
So, you can use balls1d the same was as you used to use the original array.
The real question, though, is why are you creating a 2D array in the first place? I'm supposing each ball bounces around independently, and its location has little to do with its 2D array indices.
-spxl