java 两维数组理解

1:定义

   二维数组动态初始化的语法格式:
       #1:数据类型[][] name = new 数据类型[第一维的长度][第二维的长度];数组

        例如:int[][] a;
       #2:数据类型[][] 数组名称;maven

        例如:double[] a[];
       #3: 数组名称 = new 数据类型[第一维的长度][第二维的长度];spa

        例如:char a[][];内存

静态初始化:int[][] = {{1, 2}, {3, 4}, {67, 89}};it

动态初始化:          
           byte[][] b = new byte[2][3];
           int m[][];
           m = new int[4][4];class

2:二维数组内存结构

因此:int[][] m = new int[2][3];数据类型

行数: row = m.length();语法

列数:col = m[0].length();im

3:拓展

  #1: 二维数组能够模拟矩阵的运算,对矩阵好用的jar:数据

jama.jar-------   maven地址:http://mvnrepository.com/search?q=jama

简单的实现一个矩阵加法:

/**
 *
 * @auther  cheng
 * @create 2017/11/1
 */
public class MatrixAdd {
    public static void print(int[][] c) {
        if (c == null) {
            System.out.println("矩阵为空");
            return;
        }
        for (int i = 0; i < c.length; i++) {
            for (int j = 0; j < c[0].length; j++) {
                System.out.print(c[i][j] + "\t");
            }
            System.out.println();
        }
    }

    public static int[][] add(int[][] a, int[][] b) {
        if (a == null || b == null || a.length != b.length || a.length != a[0].length || b.length != b[0].length) {
            System.out.println("矩阵不规范");
            return null;         
        }
        int[][] c = new int[a.length][a.length];
        for (int i = 0; i < c.length; i++) {
            for (int j = 0; j < c.length; j++) {
                c[i][j] = a[i][j] + b[i][j];
            }
        }
        return c;
    }

    public static void main(String[] args) {
        int[][] a = {{45, 35, 37}, {21, 41, 17}, {76, 94, 69}};
        int[][] b = {{43, 63, 73}, {2, 4, 7}, {16, 34, 98}};
        int[][] c = add(a, b);
        print(c);
    }
}

结果:

88    98    110    
23    45    24    
92    128    167    

  #2: 动态规划问题

相关文章
相关标签/搜索