有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何乘法二维数组?矩阵乘法

所以我有一个代码可以打印一个二维数组表。我遇到的问题是,我完全不知道如何乘法和求数组的乘积。感谢您的帮助。谢谢

public class MultiplyingArrays {

    public static void main(String[] args) {
        int firstarray[][] = {{1, 2, -2, 0}, {-3, 4, 7, 2}, {6, 0, 3, 1}};
        int secondarray[][] = {{-1, 3}, {0, 9}, {1, -11}, {4, -5}};

        System.out.println("This is the first array");
        display(firstarray);

        System.out.println("This is the second array");
        display(secondarray);
    }

    public static void display(int x[][]) {
        for (int row = 0; row < x.length; row++) {
            for (int column = 0; column < x[row].length; column++) {
                System.out.print(x[row][column] + "\t");
            }
            System.out.println();
        }
    }
}

预期的结果将是:

 -3   43
 18   60
  1  -20

共 (3) 个答案

  1. # 1 楼答案

           import java.util.Scanner;
           class MatrixMultiplication
      {
           public static void main(String args[])
      {
           int n;
           Scanner sc=new Scanner(System.in);
           System.out.println("Enter the base of square matrix");
           n=sc.nextInt();
           int a[][]=new int[n][n];
           int b[][]=new int[n][n];
           int c[][]=new int[n][n];
           System.out.println("Enter the elements of matrix a");
           for(int i=0;i<n;i++)
        {
           for(int j=0;j<n;j++)
        {
           a[i][j]=sc.nextInt();
        }
        }
           System.out.println("Enter the elements of matrix b");
           for(int i=0;i<n;i++)
        {
           for(j=0;j<n;j++)
        {
           b[i][j]=sc.nextInt();
        }
        }
           System.out.println("Multiplying the matrices....");
        {
           for(int i=0;i<n;i++)
        {
           for(int j=0;j<n;j++)
        {
           for(int k=0;k<n;k++)
        {
           c[i][j]=c[i][j]+a[i][k]*b[k][j];
        }
        }
        }
           System.out.println("the product is:");
           for(int i=0;i<n;i++)
        {
           for(int j=0;j<n;j++)
        {
           System.out.print(c[i][j]+"   ");
        }
           System.out.println();
        }
        }
        }
    
  2. # 2 楼答案

    int firstarray[][] = {{1, 2, -2, 0}, {-3, 4, 7, 2}, {6, 0, 3, 1}};
    int secondarray[][] = {{-1, 3}, {0, 9}, {1, -11}, {4, -5}};
    
    /* Create another 2d array to store the result using the original arrays' lengths on row and column respectively. */
    int [][] result = new int[firstarray.length][secondarray[0].length];
    
    /* Loop through each and get product, then sum up and store the value */
    for (int i = 0; i < firstarray.length; i++) { 
        for (int j = 0; j < secondarray[0].length; j++) { 
            for (int k = 0; k < firstarray[0].length; k++) { 
                result[i][j] += firstarray[i][k] * secondarray[k][j];
            }
        }
    }
    /* Show the result */
    display(result);
    

    请使用适当的naming convention

  3. # 3 楼答案

    对于喜欢这种方法的人:

    import java.util.*;
    
    public class MatmultD
    {
    private static Scanner sc = new Scanner(System.in);
      public static void main(String [] args)
      {
        int a[][] = {{1, 2, -2, 0}, {-3, 4, 7, 2}, {6, 0, 3, 1}};
        int b[][] = {{-1, 3}, {0, 9}, {1, -11}, {4, -5}};
        int[][] c=multMatrix(a,b);
        printMatrix(a);
        printMatrix(b);    
        printMatrix(c);
    
      }
    
       public static int[][] readMatrix() {
           int rows = sc.nextInt();
           int cols = sc.nextInt();
           int[][] result = new int[rows][cols];
           for (int i = 0; i < rows; i++) {
               for (int j = 0; j < cols; j++) {
                  result[i][j] = sc.nextInt();
               }
           }
           return result;
       }
    
    
      public static void printMatrix(int[][] mat) {
      System.out.println("Matrix["+mat.length+"]["+mat[0].length+"]");
           int rows = mat.length;
           int columns = mat[0].length;
           for (int i = 0; i < rows; i++) {
               for (int j = 0; j < columns; j++) {
                   System.out.printf("%4d " , mat[i][j]);
               }
               System.out.println();
           }
       System.out.println();
      }
    
       public static int[][] multMatrix(int a[][], int b[][]){//a[m][n], b[n][p]
       if(a.length == 0) return new int[0][0];
       if(a[0].length != b.length) return null; //invalid dims
    
       int n = a[0].length;
       int m = a.length;
       int p = b[0].length;
       int ans[][] = new int[m][p];
    
       for(int i = 0;i < m;i++){
          for(int j = 0;j < p;j++){
             for(int k = 0;k < n;k++){
                ans[i][j] += a[i][k] * b[k][j];
             }
          }
       }
       return ans;
       }
    }
    

    输出结果如下所示

    Matrix[3][4]
       1    2   -2    0 
      -3    4    7    2 
       6    0    3    1 
    
    Matrix[4][2]
      -1    3 
       0    9 
       1  -11 
       4   -5 
    
    Matrix[3][2]
      -3   43 
      18  -60 
       1  -20