手撸代码系列(十六)--二维数组的顺时针、逆时针的遍历

本文介绍使用Java语言实现二维数组的顺时针和逆时针遍历的方法。通过具体示例代码展示了如何从左上角开始,按顺时针或逆时针方向遍历并打印出矩阵中的所有元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java语言实现二维数组的顺时针、逆时针的遍历

在这里插入图片描述

// 逆时针打印数组
public class Main {
    public static void main(String[] args) {
        int[][] matrix = {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
                {13, 14, 15, 16}};
        print(matrix);
    }
    private static void print(int[][] matrix) {
        int leftUpRow = 0;  //左上角 行
        int leftUpCol = 0;  //左上角 列
        int rightDownRow = matrix.length-1;   //右下角 行
        int rightDownCol = matrix[0].length-1;   // 右下角 列
        while (leftUpRow <= rightDownRow  && leftUpCol <= rightDownCol){
            int r = leftUpRow; // 当前行指针
            int c = leftUpCol; // 当前列指针
            // 左边一列,列序号不动,行++
            while (r <= rightDownRow){
                System.out.print(matrix[r++][c] + " ");
            }
            // 跳出循环,说明已经超过了行,要恢复行
            r = rightDownRow;
            // 列号+1,位后续做准备
            c++;
            // 下面一行, 行序号不动,列++
            while (c <= rightDownCol){
                System.out.print(matrix[r][c++] + " ");
            }
            // 跳出循环,说明已经超过了列,要恢复行
            c = rightDownCol;
            // 行号-1,位后续做准备
            r--;
            // 右边一列, 列号不动,行--
            while (r >= leftUpRow){
                System.out.print(matrix[r--][c] + " ");
            }
            // 跳出循环,说明已经超过了行,要恢复行
            r = leftUpRow;
            // 列号-1,位后续做准备
            c--;
            // 上面一行
            while (c > leftUpCol){ // 注意,不用等号,跳出循环,正好是下一次运行的开始。
                System.out.print(matrix[r][c--] + " ");
            }
            leftUpCol++;
            leftUpRow++;
            rightDownCol--;
            rightDownRow--;
        }
    }
}
// 顺时针打印二维数组
public class Main {
    public static void main(String[] args) {
        int[][] matrix = {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
                {13, 14, 15, 16}};
        print(matrix);
    }
    private static void print(int[][] matrix) {
        int leftUpRow = 0;  //左上角 行
        int leftUpCol = 0;  //左上角 列
        int rightDownRow = matrix.length-1;   //右下角 行
        int rightDownCol = matrix[0].length-1;   // 右下角 列
        while (leftUpRow <= rightDownRow  && leftUpCol <= rightDownCol){
            int r = leftUpRow;
            int c = leftUpCol;
            // 上面一行
            while (c <= rightDownCol){
                System.out.print(matrix[r][c++] + " ");
            }
            // 恢复
            c = rightDownRow;
            r++;// 下一行
            // 右边一列
            while (r <= rightDownRow){ // 行下移,
                System.out.print(matrix[r++][c] + " ");
            }
            r = rightDownRow;
            c--;
            // 下面一行
            while (c >= leftUpCol){
                System.out.print(matrix[r][c--] + " ");
            }
            c = leftUpCol;
            r--;
            // 左边一列
            while (r > leftUpRow){
                System.out.print(matrix[r--][c] + " ");
            }
            leftUpCol++;
            leftUpRow++;
            rightDownCol--;
            rightDownRow--;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值