1. 程式人生 > >Java數組(1):數組與多維數組

Java數組(1):數組與多維數組

soft arr ring 基本 int div 標識 apple 矩陣

我們對數組的基本看法是,你可以創建它們,通過使用整型索引值訪問它們的元素,並且他們的尺寸不能改變。

但是有時候我們需要評估,到底是使用數組還是更加靈活的工具。數組是一個簡單的線性序列,這使得元素訪問非常快速。但是為這種速度所付出的代價是數組大小被固定,並且在其生命周期中不可改變。

無論使用哪種類型的數組,數組標識符只是一個引用。基本類型數組直接保存基本類型的值,對象數組保存一個指向其他對象的引用。

 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類型的數組對象,但是都指向null 13 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 Apple4 17 // Aggregate initialization: 18 Apple[] d = {new Apple(), new Apple(), new Apple()}; // Apple5 Apple6 Apple7 19 // Dynamic aggregate initialization: 20 a = new
Apple[]{new Apple(), new Apple()}; // Apple8 Apple9 21 System.out.println("a.length = " + a.length); // a.length = 2 22 System.out.println("b.length = " + b.length); // b.length = 5 23 System.out.println("c.length = " + c.length); // c.length = 4 24 System.out.println("d.length = " + d.length); // d.length = 3 25 a = d; 26 System.out.println("a.length = " + a.length); // a.length = 3 27 28 int[] e; // 創建一個基本類型的數組引用 29 int[] f = new int[5]; // 創建一個Apple基本數組對象,初始化值都為0 30 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 = 5 36 System.out.println("g.length = " + g.length); // g.length = 4 37 System.out.println("h.length = " + h.length); // h.length = 3 38 e = h; 39 System.out.println("e.length = " + e.length); // e.length = 3 40 e = new int[]{1, 2}; 41 System.out.println("e.length = " + e.length); // e.length = 2 42 } 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):數組與多維數組