範例:
- class java_ex33_array_length {
- public static void main(String[] args) {
- //取得一維陣列長度範例
- int[] a = new int[5];
- a[0] = 66;
- a[1] = 99;
- a[2] = 67;
- a[3] = 75;
- a[4] = 89;
- System.out.println("陣列 a 的長度為:" + a.length);
- //取得二維陣列長度範例
- int[][] b = new int[2][3];
- b[0][0] = 1;
- b[0][1] = 2;
- b[0][2] = 3;
- b[1][0] = 4;
- b[1][1] = 5;
- b[1][2] = 6;
- System.out.println("陣列 b 的長度(行數)為:" + b.length);
- System.out.println("陣列 b 的長度(列數)為:" + b[0].length);
- }
- }