当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>Java进阶

用Java的循环实现矩阵乘法代码分享


  public class MultiMatrix {
  int[][] multiplyMatrix;
  public static void main(String args[]){
  int[][] a={{1,0,3,-1},{2,1,0,2}};
  int[][] b={{4,1,0},{-1,1,3},{2,0,1},{1,3,4}};
  MultiMatrix mm=new MultiMatrix();
  mm.mMatrix(a,b);
  mm.display();
  
  }
  public void mMatrix(int[][] a,int[][] b){
  multiplyMatrix=new int[a.length][b[0].length];
  for (int i = 0; i<a.length; i++) {//rows of a
  for (int j = 0; j<b[0].length; j++) {//columns of b
  for (int k = 0; k<a[0].length; k++) {//columns of a = rows of b
  multiplyMatrix[i][j]=multiplyMatrix[i][j]+a[i][k]*b[k][j];
  }
  }
  }
  }
  public void display(){
  for (int i = 0; i<multiplyMatrix.length; i++) {
  for (int j = 0; j<multiplyMatrix[0].length; j++) {
  System.out.print (multiplyMatrix[i][j]+" ");
  
  }
  System.out.println ("");
  }
  }
  }
  
相关内容
赞助商链接