java——二维数组行列转换

完整代码

package 数组转化;



public class arrayTurn {

    public static void PrintArray(int [][]Array){
        for (int i = 0; i < Array.length; i++) {
            for (int j = 0; j < Array[i].length; j++) {
                System.out.print(Array[i][j]+"\t");
            }
            System.out.println();
        }

    }
    public static void main(String[] args) {
        int [][]ArrayA={{1 , 7 , 15, 12},{21, 6 , 3 , 56},{4 , 12, 8 , 11}};//三行四列
        //System.out.println(ArrayA[0].length);
        int [][]ArrayB=new int[ArrayA[0].length][ArrayA.length];//四行三列

        System.out.println("原数组:");
        PrintArray(ArrayA);

        System.out.println("==================================");
        //转化
        for (int i = 0; i < ArrayA.length; i++) {
            for (int j = 0; j < ArrayA[i].length; j++) {
                ArrayB[j][i]=ArrayA[i][j];
            }

        }
        System.out.println("转化后:");
        PrintArray(ArrayB);
        System.out.println();

    }
}

运行效果:

java——二维数组行列转换

猜你喜欢