<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 一等對象 不管你使用的什么類型的數組,數組中的數據集實際上都是對堆中真正對象的引用。數組是保存指向其他對象的引用的對象,數組可以隱式地創建,作為數組初始化語法的一部分,也可以顯式地創建,比如使用一個 **new** 表達式。數組對象的一部分(事實上,你唯一可以使用的方法)就是只讀的 **length** 成員函數,它能告訴你數組對象中可以存儲多少元素。**[ ]** 語法是你訪問數組對象的唯一方式。 下面的例子總結了初始化數組的多種方式,并且展示了如何給不同的數組對象分配數組引用。同時也可以看出對象數組和基元數組在使用上是完全相同的。唯一的不同之處就是對象數組存儲的是對象的引用,而基元數組則直接存儲基本數據類型的值。 ```java // arrays/ArrayOptions.java // Initialization & re-assignment of arrays import java.util.*; import static onjava.ArrayShow.*; public class ArrayOptions { public static void main(String[] args) { // Arrays of objects: BerylliumSphere[] a; // Uninitialized local BerylliumSphere[] b = new BerylliumSphere[5]; // The references inside the array are // automatically initialized to null: show("b", b); BerylliumSphere[] c = new BerylliumSphere[4]; for(int i = 0; i < c.length; i++) if(c[i] == null) // Can test for null reference c[i] = new BerylliumSphere(); // Aggregate initialization: BerylliumSphere[] d = { new BerylliumSphere(), new BerylliumSphere(), new BerylliumSphere() }; // Dynamic aggregate initialization: a = new BerylliumSphere[]{ new BerylliumSphere(), new BerylliumSphere(), }; // (Trailing comma is optional) System.out.println("a.length = " + a.length); System.out.println("b.length = " + b.length); System.out.println("c.length = " + c.length); System.out.println("d.length = " + d.length); a = d; System.out.println("a.length = " + a.length); // Arrays of primitives: int[] e; // Null reference int[] f = new int[5]; // The primitives inside the array are // automatically initialized to zero: show("f", f); int[] g = new int[4]; for(int i = 0; i < g.length; i++) g[i] = i*i; int[] h = { 11, 47, 93 }; // Compile error: variable e not initialized: //- System.out.println("e.length = " + e.length); System.out.println("f.length = " + f.length); System.out.println("g.length = " + g.length); System.out.println("h.length = " + h.length); e = h; System.out.println("e.length = " + e.length); e = new int[]{ 1, 2 }; System.out.println("e.length = " + e.length); } } /* Output: b: [null, null, null, null, null] a.length = 2 b.length = 5 c.length = 4 d.length = 3 a.length = 3 f: [0, 0, 0, 0, 0] f.length = 5 g.length = 4 h.length = 3 e.length = 3 e.length = 2 */ ``` 數組 **a** 是一個未初始化的本地變量,編譯器不會允許你使用這個引用直到你正確地對其進行初始化。數組 **b** 被初始化成一系列指向 **BerylliumSphere** 對象的引用,但是并沒有真正的 **BerylliumSphere** 對象被存儲在數組中。盡管你仍然可以獲得這個數組的大小,因為 **b** 指向合法對象。這帶來了一個小問題:你無法找出到底有多少元素存儲在數組中,因為 **length** 只能告訴你數組可以存儲多少元素;這就是說,數組對象的大小并不是真正存儲在數組中對象的個數。然而,當你創建一個數組對象,其引用將自動初始化為 **null**,因此你可以通過檢查特定數組元素中的引用是否為 **null** 來判斷其中是否有對象。基元數組也有類似的機制,比如自動將數值類型初始化為 **0**,char 型初始化為 **(char)0**,布爾類型初始化為 **false**。 數組 **c** 展示了創建數組對象后給數組中各元素分配 **BerylliumSphere** 對象。數組 **d** 展示了創建數組對象的聚合初始化語法(隱式地使用 **new** 在堆中創建對象,就像 **c** 一樣)并且初始化成 **BeryliumSphere** 對象,這一切都在一條語句中完成。 下一個數組初始化可以被看做是一個“動態聚合初始化”。 **d** 使用的聚合初始化必須在 **d** 定義處使用,但是使用第二種語法,你可以在任何地方創建和初始化數組對象。例如,假設 **hide()** 是一個需要使用一系列的 **BeryliumSphere**對象。你可以這樣調用它: ```Java hide(d); ``` 你也可以動態地創建你用作參數傳遞的數組: ```Java hide(new BerylliumSphere[]{ new BerlliumSphere(), new BerlliumSphere() }); ``` 很多情況下這種語法寫代碼更加方便。 表達式: ```Java a = d; ``` 顯示了你如何獲取指向一個數組對象的引用并將其分配給另一個數組對象。就像你可以處理其他類型的對象引用。現在 **a** 和 **d** 都指向了堆中的同一個數組對象。 **ArrayOptions.java** 的第二部分展示了基元數組的語法就像對象數組一樣,除了基元數組直接保存基本數據類型的值。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看