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

Java矩阵乘法Multiply函数返回矩阵形式结果的问题排查与实现指导

解答你的矩阵乘法问题与OOP小建议

Hey there! Let's break down your questions one by one—since you're learning OOP and new to Stack Overflow-style help, I'll keep things clear and step-by-step.

1. 首先:[[F@1cd072a9] 不是算法错误!

That weird output you're seeing is just Java's default way of printing array objects. Here's what it actually means:

  • [[F tells you this is a 2-dimensional float array ([ represents each dimension, F stands for float)
  • @1cd072a9 is the array's hash code (a unique memory identifier for the object)

You get this because System.out.println(result); prints the array object itself, not its contents. To see the actual matrix values, you need to either iterate through the array manually or use Java's built-in Arrays.deepToString() method.

修复打印问题:

  1. Add this import at the top of your matrix class:
    import java.util.Arrays;
    
  2. Replace your print line with:
    System.out.println(Arrays.deepToString(result));
    

This will print the full matrix with its actual float values instead of the cryptic hash code.

2. 你的矩阵乘法算法有潜在问题

Looking at your multiply() method, there are two small but important issues to fix:

问题1:循环维度逻辑不够严谨

Your third loop uses k < getM2().length—this works for your test case (3x2 * 2x3 matrix), but it's not universally correct. For matrix multiplication, the third loop should run from 0 to the number of columns in M1 (or rows in M2, since they must be equal). A safer approach is to store matrix dimensions in local variables first:

float[][] m1 = getM1();
float[][] m2 = getM2();
// 第三个循环用 m1[0].length(M1的列数)或 m2.length(M2的行数)—— 两者必须相等才能相乘
for(int k = 0; k < m1[0].length; k++) {
    result[i][j] += m1[i][k] * m2[k][j];
}

问题2:缺少矩阵合法性校验

Matrix multiplication only works if the number of columns in M1 equals the number of rows in M2. Right now, if someone passes invalid matrices (like 3x3 * 2x2), your code will throw an ArrayIndexOutOfBoundsException. Add a check at the start of multiply() to make this clear:

if (m1 == null || m2 == null || m1[0].length != m2.length) {
    throw new IllegalArgumentException("无法执行矩阵乘法:第一个矩阵的列数必须等于第二个矩阵的行数!");
}

3. OOP基础的小建议(符合Java规范)

Since you're learning OOP, here are some quick tips to make your code more idiomatic:

  • Class names should start with a capital letter: matrix → Matrix, calculate → Calculate (this is a core Java naming convention that makes code easier to read)
  • Encapsulate logic better: Instead of calling getM1() and getM2() multiple times in loops, store their values in local variables (like we did above)—it's more efficient and readable.
  • Add meaningful constructors: Instead of using setM1() and setM2() after creating an object, add a constructor that takes both matrices as parameters:
    public Matrix(float[][] M1, float[][] M2) {
        this.M1 = M1;
        this.M2 = M2;
    }
    
    Then in your main class, you can create an object like this:
    Matrix matrix1 = new Matrix(m1, m2);
    

修改后的完整代码示例

Here's how your Matrix class could look with all these fixes:

package matrixmultiply;
import java.util.Arrays;

public class Matrix extends Calculate {
    private float[][] M1;
    private float[][] M2;

    // 新增构造方法
    public Matrix(float[][] M1, float[][] M2) {
        this.M1 = M1;
        this.M2 = M2;
    }

    @Override
    public float[][] multiply() {
        float[][] m1 = getM1();
        float[][] m2 = getM2();

        // 校验矩阵合法性
        if (m1 == null || m2 == null || m1[0].length != m2.length) {
            throw new IllegalArgumentException("无法执行矩阵乘法:第一个矩阵的列数必须等于第二个矩阵的行数!");
        }

        float[][] result = new float[m1.length][m2[0].length];
        for(int i = 0; i < m1.length; i++) {
            for(int j = 0; j < m2[0].length; j++) {
                for(int k = 0; k < m1[0].length; k++) {
                    result[i][j] += m1[i][k] * m2[k][j];
                }
            }
        }

        // 正确打印矩阵
        System.out.println(Arrays.deepToString(result));
        return result;
    }

    // Getters and Setters
    public float[][] getM1() {
        return M1;
    }

    public void setM1(float[][] M1) {
        this.M1 = M1;
    }

    public float[][] getM2() {
        return M2;
    }

    public void setM2(float[][] M2) {
        this.M2 = M2;
    }
}

And your main class with sample matrix values (so you can see actual results):

package matrixmultiply;

public class MatrixMultiply {
    public static void main(String[] args) {
        // 给矩阵赋值,而不是用空的默认值
        float[][] m1 = {{1, 2}, {3, 4}, {5, 6}};
        float[][] m2 = {{7, 8, 9}, {10, 11, 12}};
        
        Matrix matrix1 = new Matrix(m1, m2);
        matrix1.multiply();
        // 输出应该是:[[27.0, 30.0, 33.0], [61.0, 68.0, 75.0], [95.0, 106.0, 117.0]]
    }
}

内容的提问来源于stack exchange,提问作者Murat-Dayan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 15:17:30