二維數組動態初始化的語法格式:
#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
因此:int[][] m = new int[2][3];數據類型
行數: row = m.length();語法
列數:col = m[0].length();im
#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: 動態規劃問題