您的位置:首页 > 编程语言 > Java开发

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

2009-05-18 19:22 726 查看
 public class MultiMatrix {
 int[][] multiplyMatrix;

 public static void main(String args[]) {
  int[][] a = {
    { 1, 0, 3, -1,2,5,8},
    { 2, 1, 0, 2,7,3,5},
    {2,3,4,5,1,0,3},
    { 1, 0, 3, -1,2,5,8},
    { 2, 1, 0, 2,7,3,5},
    {2,3,4,5,1,0,3},
    { 2, 1, 0, 2,7,3,5},
    {2,3,4,5,1,0,3}
  };
  int[][] b = {
    {4,1},
    {-1,1},
    {2,0},
    {4,1},
    {-1,1},
    {2,0},
    {1,3}
    
    };
  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("");
  }
 }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java string class