標籤:列印 gate blog gpo ini mic 使用 for 方法
我們對數組的基本看法是,你可以建立它們,通過使用整型索引值訪問它們的元素,並且他們的尺寸不能改變。
但是有時候我們需要評估,到底是使用數組還是更加靈活的工具。數組是一個簡單的線性序列,這使得元素訪問非常快速。但是為這種速度所付出的代價是數組大小被固定,並且在其生命週期中不可改變。
無論使用哪種類型的數組,數群組識別碼只是一個引用。基本類型數組直接儲存基本類型的值,對象數組儲存一個指向其他對象的引用。
1 class Apple { 2 private static int count = 1; 3 4 public Apple() { 5 System.out.println("Apple" + count++); 6 } 7 } 8 9 public class ArrayOptions {10 public static void main(String[] args) {11 Apple[] a; // 建立一個Apple類型的數組引用12 Apple[] b = new Apple[5]; // 建立一個Apple類型的數組對象,但是都指向null13 System.out.println("b: " + Arrays.toString(b)); // b: [null, null, null, null, null]14 Apple[] c = new Apple[4];15 for (int i = 0; i < c.length; i++)16 c[i] = new Apple(); // Apple1 Apple2 Apple3 Apple417 // Aggregate initialization:18 Apple[] d = {new Apple(), new Apple(), new Apple()}; // Apple5 Apple6 Apple719 // Dynamic aggregate initialization:20 a = new Apple[]{new Apple(), new Apple()}; // Apple8 Apple921 System.out.println("a.length = " + a.length); // a.length = 222 System.out.println("b.length = " + b.length); // b.length = 523 System.out.println("c.length = " + c.length); // c.length = 424 System.out.println("d.length = " + d.length); // d.length = 325 a = d;26 System.out.println("a.length = " + a.length); // a.length = 327 28 int[] e; // 建立一個基本類型的數組引用29 int[] f = new int[5]; // 建立一個Apple基本數組對象,初始化值都為030 System.out.println("f: " + Arrays.toString(f)); // f: [0, 0, 0, 0, 0]31 int[] g = new int[4];32 for (int i = 0; i < g.length; i++)33 g[i] = i * i;34 int[] h = {11, 47, 93};35 System.out.println("f.length = " + f.length); // f.length = 536 System.out.println("g.length = " + g.length); // g.length = 437 System.out.println("h.length = " + h.length); // h.length = 338 e = h;39 System.out.println("e.length = " + e.length); // e.length = 340 e = new int[]{1, 2};41 System.out.println("e.length = " + e.length); // e.length = 242 }43 }
多維陣列
1 import java.util.Arrays; 2 3 class Apple { 4 private static int count = 1; 5 6 public String toString() { 7 return "Apple" + count++; 8 } 9 }10 11 public class MutiArray {12 public static void main(String[] args) {13 // (1) 使用Arrays.deepToString()可以列印多維陣列14 int[][] a = {{1, 2, 3}, {4, 5, 6}};15 long[][][] b = new long[2][2][3];16 System.out.println(Arrays.deepToString(a)); // [[1, 2, 3], [4, 5, 6]]17 System.out.println(Arrays.deepToString(b)); // [[[0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0]]]18 19 // (2) 多維陣列值的設定可以用下面方法,並且數組中構成矩陣的每個向量都可具有任意長度(粗糙數組)20 int[][] c = new int[3][];21 for (int i = 0; i < c.length; i++) {22 c[i] = new int[i + 1];23 for (int j = 0; j < c[i].length; j++) {24 c[i][j] = j + 1;25 }26 }27 System.out.println(Arrays.deepToString(c)); // [[1], [1, 2], [1, 2, 3]]28 29 // (3) 用類似方式還可以處理非基本類型的(封裝類和對象類)數組30 Apple[][] d = {{new Apple(), new Apple()}, {new Apple(), new Apple(), new Apple()}};31 Integer[][] e = {{1, 2}, {3, 4, 5}, {6, 7}};32 System.out.println(Arrays.deepToString(d)); // [[Apple1, Apple2], [Apple3, Apple4, Apple5]]33 System.out.println(Arrays.deepToString(e)); // [[1, 2], [3, 4, 5], [6, 7]]34 }35 }
Java數組(1):數組與多維陣列