Transposing a matrix from a 2D array

后端 未结 5 2120
借酒劲吻你
借酒劲吻你 2021-01-06 19:38

I\'m self teaching myself some java and I\'m stuck on creating a 2D array that initializes it with random values and then creates the transpose of the array.

An ex

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-06 20:16

    For a square matrix, instead of iterating through the entire array, you just iterate through the diagonally half of the 2D array and swap the values with the corresponding indices.

    public void transposeMatrix(int[][] a) {
            for(int i=0 ; i

提交回复
热议问题