Massive Swap - 2d Array

I am working on replacing indexes in a two-dimensional array. I seem to be on the right track, but it does not change the array the way I want.

The index of the first row j should be replaced by the index of row 2 j :

 for (int j = 0; j < array.length ; j++){ int temp = array[row1][j] array[row1][j]=array[j][row1] array[j][row1] = temp ; } 

Any ideas on how best to approach this would be appreciated.

+4
source share
1 answer

Since a two-dimensional array in java is actually an array of references to other arrays, you can simply replace the links as shown below:

 public static void swapRows(int array[][], int rowA, int rowB) { int tmpRow[] = array[rowA]; array[rowA] = array[rowB]; array[rowB] = tmpRow; } 

/ edit : edited the answer since I misinterpreted it earlier **

+2
source

Source: https://habr.com/ru/post/1444699/


All Articles