I need to change the values ​​in the char[][] array around the cell. Is it possible to do so? It is advisable to check for exit from the array.

Closed due to the fact that the essence of the question is incomprehensible by the participants Roman C , Eugene Krivenja , Alex , user300000, aleksandr barakin 8 Oct '18 at 0:05 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What confuses you if the concept of a cycle is already familiar? - MBo

1 answer 1

Bypassing eight adjacent cells

 char[][] array = ... int i0 = ... int j0 = ... int height = array.length; int width = array[0].length; for (int i = i0 - 1; i <= i0 + 1; ++i) { for (int j = j0 - 1; j <= j0 + 1; ++j) { // проверка на выход за границы массива // и проверка на то, что обрабатываемая ячейка не равна изначальной ячейке if (0 <= i && i < height && 0 <= j && j < width && (i != i0 || j != j0)) { // любая операция с соседним элементом array[i][j]++; } } } 

Bypassing four neighboring cells

You can add to the previous method that the cell ( i , j ) is adjacent to the side with the cell ( i0 , j0 ):

 i == i0 || j == j0 

Or, put the method of changing the neighboring cell into a separate method and call it four times:

 void modifyCell(char[][] array, int i, int j) { if (0 <= i && i < array.length && 0 <= j && j < array[i].length) { // любая операция с соседним элементом array[i][j]++; } } // в нужном месте вызываем метод: modifyCell(array, i0 - 1, j0); modifyCell(array, i0, j0 - 1); modifyCell(array, i0, j0 + 1); modifyCell(array, i0 + 1, j0);