The matrix is ​​shifted, but to the place where the extreme columns should move, the rightmost row is simply copied to the specified number of shifts.

static void ShiftLeft(short arr[][], int position) { for (int k = 0; k < position; k++) for(int i = arr.length-1; i >= 0; i--) { short temp = arr[i][arr[i].length-1]; int j; for(j = 1; j < arr[i].length; j++) { arr[i][j-1] = arr[i][j]; } arr[i][j-1] = temp; } } 

    1 answer 1

     public static void main(String[] args) { short[][] m = {{1,2,3},{4,5,6},{7,8,9}}; showMatrix(m); //shiftLeft(m, 5); shiftLeft2(m, 5); System.out.println(); showMatrix(m); } // Вариант с пошаговым переставлением. public static void shiftLeft(short[][] m, int steps) { for (int i=0; i<m.length; i++) { // количество реальных шагов, без лишних оборотов. // в условии не гарантируется одинаковое количество столбцов // для всех строк, поэтому реальные шаги определяются для каждой строки int realSteps = steps % m[i].length; while(realSteps-- > 0) { short temp = m[i][0]; for (int j=m[i].length-1; j>=0; j--) { short val = m[i][j]; m[i][j] = temp; temp = val; } } } } // Вариант с доп массивом. Быстрее, но затраты памяти на доп.массив public static void shiftLeft2(short[][] m, int steps) { for (int i=0; i<m.length; i++) { int realSteps = steps % m[i].length; if(realSteps == 0) continue; int rowLength = m[i].length; short[] temp = new short[rowLength]; for (int j=0; j < rowLength; j++) { int newIndex = (j-realSteps<0) ? rowLength+j-realSteps : j-realSteps; temp[newIndex] = m[i][j]; } for (int j=0; j<rowLength; j++ ) { m[i][j] = temp[j]; } } } public static void showMatrix(short[][] m) { for (short[] s : m) { for (short k : s) { System.out.print(k + " "); } System.out.println(); } } 

    Result

     1 2 3 4 5 6 7 8 9 3 1 2 6 4 5 9 7 8