The ranks of the exchange of two-dimensional array (Java programming classical case)

Data table may store two-dimensional array, the array may also be in accordance with various arithmetic subscripting added. Image calculation method is based on a two-dimensional array of matrix operation, the data table of the present example of an analog exchange rows and columns, as follows:

public class Test {
    public static void main(String[] args) {
        int arr[][] = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        System.out.println("行列互调前:");
        // 输出二维数组
        printArray(arr);
        int arr2[][] = new int[arr.length][arr.length];
        for (int i = 0; i < arr.length; i++) {// 调整数组行列数据
            for (int j = 0; j < arr[i].length; j++) {
                arr2[i][j] = arr[j][i];
            }
        }
        System.out.println("行列互调后:");
        // 输出行列互调后的二维数组
        printArray(arr2);
    }

    /**
     * 遍历数组并输出数组的全部元素
     */
    private static void printArray(int[][] arr) {
        for (int i = 0; i < arr.length; i++) {// 遍历数组
            for (int j = 0; j < arr.length; j++) {
                System.out.print(arr[i][j] + " ");// 不换行输出数组元素
            }
            System.out.println();	//换行
        }
    }
}

Execution results as shown below:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/cui_yonghua/article/details/93376530