문제

I have an 2D array:

int[,] twoArr=  new int[2,10];

Let's say that I am at position 0,8 in the array and the user enters that they want to get the value of current position + 3. How do you do that?

I only know the position I am standing at, but I don't know which value the user enter, they could enter 1,2,3,4... etc.

The thing is that I don't know how to make the jump from position:

0,9 - 1,0 - 1,1...

도움이 되었습니까?

해결책

I think you can try the following:

int max_n = 2;
int max_m = 10;
int current_position_x = 0;
int current_position_y = 8;
current_position_y += 3;   // here you add a shift to your current position
if (current_position_y >= max_m)
{
     current_position_x += current_position_y / max_m;
     current_position_y %= max_m;
}

In this case you will go over the array as (0, 0), (0, 1) ... (0, 9), (1, 0) ... (1, 9)

다른 팁

Back to the "old" days when all arrays were 1 dimensional, which they still are really.

Position 0,8 = Row * 0 + Column = 8

Position 8 + 3 = 11, so row == 11 div (Number of columns) [10] = 1 and column = 11 % 10 = 1

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top