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

Java二维数组列相似性判断逻辑错误排查求助

二维数组相似列判断逻辑错误排查

需求说明

  • 已实现numInColumn函数:接收矩阵、列号和值,统计该值在指定列中的出现次数
  • 需要实现similarColumns函数:复用numInColumn判断两列是否相似,相似需同时满足:
    • 每列中每个值仅出现一次
    • 两列包含完全相同的元素(顺序无关)

现有代码

import java.util.Scanner;
public class Ex_4 {
    public static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        int drill;
        System.out.println("Please enter the drill number you would like to test:");
        drill = input.nextInt();
        switch (drill) {
            case (1) -> {
                int[][] num = createMatrix();
                System.out.print("Please enter a value and the number of a column in the matrix: ");
                // please refer to the first column as 0 and not 1.
                int val = input.nextInt();
                int col = input.nextInt();
                System.out.printf("The amount of times this value shows in the column is: %d", numInColumn(num, col, val));
            }
            case (2) -> {
                int[][] similar = createMatrix();
                System.out.print("Please enter the number of two columns in the matrix: ");
                // please refer to the first column as 0 and not 1.
                int col1 = input.nextInt();
                int col2 = input.nextInt();
                if (similarColumns(similar, col1, col2)) {
                    System.out.println("true");
                } else {
                    System.out.println("false");
                }
            }
        }
    }

    public static int[][] createMatrix() {
        System.out.print("Please enter the number of rows of the matrix: ");
        int rows = input.nextInt();
        System.out.print("Please enter the number of columns of the matrix: ");
        int columns = input.nextInt();
        int[][] matrix = new int[rows][columns];
        System.out.println("Please enter the values of the matrix:");
        int j, i;
        for (i = 0; i < matrix.length; i++) {
            for (j = 0; j < matrix[0].length; j++) {
                /*
                The user needs to enter values of the matrix in this order for example:
                row 1: column 1 -> column 2.
                row 2: column 1 ->column 2.
                 */
                matrix[i][j] = input.nextInt();
            }
        }
        return matrix;
    }

    public static int numInColumn(int[][] num, int col, int val) {
        int count = 0;
        for (int i = 0; i < num.length; i++) {
            if (num[i][col] == val) {
                count++;
            }
        }
        return count;
    }

    public static boolean similarColumns(int[][] similar, int col1, int col2) {
        boolean isSimilar = true;
        for (int i = 0; i < similar.length; i++) {
                if ((numInColumn(similar, col1, similar[i][col2]) != 1)) {
                    isSimilar = false;
                }
        }
        return isSimilar;
    }
}

测试用例

测试用例1(预期返回false,实际返回true)

4 5 4 5 
3 7 3 2 
2 4 2 9 
2 9 2 5 
1 2 1 4

判断列1和列3:列1元素为[5,7,4,9,2](无重复),列3元素为[5,2,9,5,4](元素5重复出现2次),不符合相似条件,但代码返回true。

测试用例2(预期返回true,实际结果正确)

4 5 4 7 
3 7 3 2 
2 4 2 9 
2 9 2 5 
1 2 1 4

判断列1和列3:列1元素[5,7,4,9,2],列3元素[7,2,9,5,4],两列均无重复且元素完全相同,代码返回true。

逻辑错误分析

当前similarColumns函数的逻辑存在两处核心遗漏:

  1. 未检查两列自身是否存在重复元素:相似定义要求每列的每个值仅出现一次,但代码完全没验证列1、列2内部的元素重复情况。比如测试用例1中列3的元素5重复,但代码不会检测到这个问题。
  2. 仅单向验证元素存在性:代码只检查了列2的元素在列1中的出现次数,没有确保列1的元素也都存在于列2中(虽然在两列长度相同且无重复的前提下,单向检查可以覆盖,但前提是必须先验证两列无重复)。

针对测试用例1的具体执行过程:循环遍历列3的每个元素,检查其在列1中的出现次数:

  • 元素5在列1出现1次 → 符合条件
  • 元素2在列1出现1次 → 符合条件
  • 元素9在列1出现1次 → 符合条件
  • 元素5在列1出现1次 → 符合条件
  • 元素4在列1出现1次 → 符合条件
    整个循环不会触发isSimilar = false,最终错误返回true,但列3自身存在重复元素,根本不满足相似的前置条件。

修复思路

要满足相似定义,需要依次完成三个检查:

  1. 验证列1中每个元素的出现次数都是1(列1无重复)
  2. 验证列2中每个元素的出现次数都是1(列2无重复)
  3. 验证列2的每个元素在列1中出现次数为1(确保两列元素完全匹配)

修改后的similarColumns函数示例:

public static boolean similarColumns(int[][] similar, int col1, int col2) {
    // 检查列1是否存在重复元素
    for (int i = 0; i < similar.length; i++) {
        if (numInColumn(similar, col1, similar[i][col1]) != 1) {
            return false;
        }
    }
    // 检查列2是否存在重复元素
    for (int i = 0; i < similar.length; i++) {
        if (numInColumn(similar, col2, similar[i][col2]) != 1) {
            return false;
        }
    }
    // 检查列2的所有元素都存在于列1中
    for (int i = 0; i < similar.length; i++) {
        if (numInColumn(similar, col1, similar[i][col2]) != 1) {
            return false;
        }
    }
    return true;
}

修改后,测试用例1在检查列2(列3)的重复元素时,会发现元素5的出现次数为2,直接返回false,符合预期。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 01:10:27