java开始到熟悉61

                              本此主题:多维数组----矩阵运算

矩阵的运算规则是将对应位置的值进行运算,如上图所示。

 1 package array;
 2 
 3 public class Matrix {
 4     /**
 5      * 打印矩阵
 6      * @param c
 7      */
 8     public static void print(int[][] c)
 9     {
10         int i,j;
11         for(i=0;i<c.length;i++)
12         {
13             for(j=0;j<c.length;j++)
14             {
15                 System.out.print(c[i][j]+"\t");
16             }
17             System.out.println();
18         }
19     }
20     /**
21      * 矩阵加法运算
22      * @param a
23      * @param b
24      * @return
25      */
26     public static int[][] add(int[][] a,int[][] b)
27     {
28         int[][] c=new int[a.length][b.length];
29         int i,j;
30         for(i=0;i<a.length;i++)
31         {
32             for(j=0;j<a.length;j++)
33             {
34                 c[i][j]=a[i][j]+b[i][j];
35             }
36         }
37         return c;
38     }
39     public static void main(String[] args)
40     {
41         int[][] a={
42                     {1,3},
43                     {2,4}
44                   };
45         int[][] b={
46                     {3,4},
47                     {5,6}
48                  };
49         int[][] c=add(a,b);
50         print(c);
51     }
52 }


运行结果:

4   7 
7   10 

posted on 2014-04-29 16:40  会敲键盘的猩猩  阅读(163)  评论(0编辑  收藏  举报