You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Java代码判断二维数组的行与列是否为回文?

问题说明

需要判断存储String或Character类型的二维数组(论坛里提到的**MATRIX(矩阵)**就是二维数组,不用困惑)的每一行和每一列是否为回文。仅掌握二维数组与循环相关知识,编写的Java代码输出不符合预期,需要实现思路讲解与代码修正指导。

原代码

public class Hard {
    public static boolean isLetterPal (String[][] array){
        int i = 0;
        int j = array.length - 1;
        while (i < j) {
            if (array.length != array.length) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }

    public static void main(String[] args) {
        
        String[][] array = { { "A", "B", "A", "B", "A" },
                             { "B", "B", "A", "B", "A" },
                             { "A", "B", "A", "B", "B" },
                             { "B", "A", "B", "B", "A" } };
        System.out.println("The array is: ");
        for (int indRow = 0; indRow < array.length; indRow++) {
            for (int indCol = 0; indCol < array[indRow].length; indCol++) {
                System.out.print(array[indRow][indCol] + " ");
                if (indCol == array[indRow].length - 1) {
                    System.out.println();
                }
            }
            System.out.println("\t");
        }
            for(int k = 0; k < array.length; k++){
                int i=0;
                if(isLetterPal(array)){
                    System.out.println("The word " + array[k][i] + " is a palindrome.");
                }else{
                    System.out.println("The word " + array[k][i] + " is not a palindrome.");
                }
            }
        }
    }

原代码问题分析

  1. isLetterPal方法逻辑完全失效:array.length != array.length是恒假判断,循环仅做指针自增自减,最终直接返回true,导致所有判断都输出“是回文”。
  2. 行判断逻辑错误:遍历行时只取了每行第一个元素(array[k][i],i固定为0),没有对整行的元素做回文校验。
  3. 缺失列的回文校验逻辑,完全没处理列的判断需求。

实现思路
  1. 回文判断逻辑:对一行/一列,用双指针法——一个从头部开始,一个从尾部开始,逐一比较对应位置的元素,全部相等则为回文,否则不是。
  2. 行校验:遍历二维数组的每一行,对单独一行调用回文判断方法,输出该行的判断结果。
  3. 列校验:
    • 先确保二维数组是矩形(所有行长度相同),否则无法统一检查列。
    • 遍历每一列,对该列的所有元素做双指针比较,输出该列的判断结果。

修正后的代码
public class PalindromeMatrix {
    // 判断单行是否为回文
    public static boolean isRowPalindrome(String[] row) {
        int left = 0;
        int right = row.length - 1;
        while (left < right) {
            // 比较左右指针位置的元素
            if (!row[left].equals(row[right])) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    // 判断单列是否为回文,需确保数组是矩形(所有行长度相同)
    public static boolean isColumnPalindrome(String[][] matrix, int colIndex) {
        int top = 0;
        int bottom = matrix.length - 1;
        while (top < bottom) {
            // 比较上下指针位置的元素
            if (!matrix[top][colIndex].equals(matrix[bottom][colIndex])) {
                return false;
            }
            top++;
            bottom--;
        }
        return true;
    }

    public static void main(String[] args) {
        String[][] array = {
                {"A", "B", "A", "B", "A"},
                {"B", "B", "A", "B", "A"},
                {"A", "B", "A", "B", "B"},
                {"B", "A", "B", "B", "A"}
        };

        // 打印数组
        System.out.println("二维数组内容:");
        for (String[] row : array) {
            for (String elem : row) {
                System.out.print(elem + " ");
            }
            System.out.println();
        }
        System.out.println();

        // 检查每一行是否为回文
        System.out.println("行回文判断结果:");
        for (int i = 0; i < array.length; i++) {
            boolean isPal = isRowPalindrome(array[i]);
            System.out.printf("第%d行:%s%n", i + 1, isPal ? "是回文" : "不是回文");
        }
        System.out.println();

        // 检查每一列是否为回文,先判断是否为矩形数组
        boolean isRectangle = true;
        int colCount = array[0].length;
        for (String[] row : array) {
            if (row.length != colCount) {
                isRectangle = false;
                break;
            }
        }
        if (!isRectangle) {
            System.out.println("数组不是矩形,无法检查列回文");
            return;
        }

        System.out.println("列回文判断结果:");
        for (int j = 0; j < colCount; j++) {
            boolean isPal = isColumnPalindrome(array, j);
            System.out.printf("第%d列:%s%n", j + 1, isPal ? "是回文" : "不是回文");
        }
    }
}

代码说明

  1. 拆分了isRowPalindrome和isColumnPalindrome两个方法,分别处理行和列的回文判断,逻辑清晰。
  2. 打印数组时简化了循环写法,更易读。
  3. 增加了矩形数组判断,避免列长度不一致导致的错误。
  4. 输出结果明确标注行/列编号和判断结果,符合需求。

内容的提问来源于stack exchange,提问作者bansannn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 18:31:07