Reputation: 2163
i have tried to split a ByteArray (that represents a screenShot Data) into a 2d array so if i have for example an array of 3x3 (xy /rows columns)
it would look like this
int screenSwidth = 3;
int screenSheight = 3;
byte[] ScreenShot = new byte[] {1,2,3,4,5,6,7,8,9};
i am trying via 2 nested for loops, to split the serial data into separated rows in a new 2dimentional array as it was shaped/formed originaly on the screen.
lets Just call the new ScreenShotRows[] as "b" just for this example
b[0][0] = 1 b[0][1] = 2 b[0][2] = 3
b[1][0] = 4 b[1][1] = 5 b[1][2] = 6
b[2][0] = 7 b[2][1] = 8 b[2][2] = 9
and the question is what is the right way to iterate through the whole array i was trying to achieve that via this code .
for (int HeightIter= 0; HeightIter < screenSheight; HeightIter++)
{
for (int WidthIter = 0; WidthIter < screenSwidth ; WidthIter++)
{
ScreenShotRows[HeightIter, WidthIter] = ScreenShot[WidthIter];
}
}
and it loops over the first row , asigning the values :
b[0][0] = 1 b[0][1] = 2 b[0][2] = 3
b[1][0] = 1 b[0][1] = 2 b[0][2] = 3
b[2][0] = 1 b[0][1] = 2 b[0][2] = 3
this is my first attempt on this kind of multiDimetional /jagged array with nested for loop, and i found it very confiusing to make it work
also to get it done as fast as it could be,
cause my data is much larger than 3x3 , and method is called multiple times frequently so performance is crucial
my next move will be to have same way with the columns
elements 0,0 to 0,2 ---- elements 1,0 to 1,2 ---- ements 2,0 to 2,2
147 258 369
Upvotes: 0
Views: 1417
Reputation: 3333
Replace your firsts line to:
for (int HeightIter= 0; HeightIter < screenSheight; HeightIter++)
And the line ScreenShotRows[HeightIter, WidthIter] = ScreenShot[WidthIter];
to
ScreenShotRows[HeightIter, WidthIter] = ScreenShot[3*HeightIter+WidthIter];
This should make 2d array.
Upvotes: 2