<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 多維數組 要創建多維的基元數組,你要用大括號來界定數組中的向量: ```Java // arrays/MultidimensionalPrimitiveArray.java import java.util.*; public class MultidimensionalPrimitiveArray { public static void main(String[] args) { int[][] a = { { 1, 2, 3, }, { 4, 5, 6, }, }; System.out.println(Arrays.deepToString(a)); } } /* Output: [[1, 2, 3], [4, 5, 6]] */。 ``` 每個嵌套的大括號都代表了數組的一個維度。 這個例子使用 **Arrays.deepToString()** 方法,將多維數組轉換成 **String** 類型,就像輸出中顯示的那樣。 你也可以使用 **new** 分配數組。這是一個使用 **new** 表達式分配的三維數組: ```Java // arrays/ThreeDWithNew.java import java.util.*; public class ThreeDWithNew { public static void main(String[] args) { // 3-D array with fixed length: int[][][] a = new int[2][2][4]; System.out.println(Arrays.deepToString(a)); } } /* Output: [[[0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0]]] */ ``` 倘若你不對基元數組進行顯式的初始化,它的值會自動初始化。而對象數組將被初始化為 **null** 。 組成矩陣的數組中每一個向量都可以是任意長度的(這叫做不規則數組): ```Java // arrays/RaggedArray.java import java.util.*; public class RaggedArray { static int val = 1; public static void main(String[] args) { SplittableRandom rand = new SplittableRandom(47); // 3-D array with varied-length vectors: int[][][] a = new int[rand.nextInt(7)][][]; for(int i = 0; i < a.length; i++) { a[i] = new int[rand.nextInt(5)][]; for(int j = 0; j < a[i].length; j++) { a[i][j] = new int[rand.nextInt(5)]; Arrays.setAll(a[i][j], n -> val++); // [1] } } System.out.println(Arrays.deepToString(a)); } } /* Output: [[[1], []], [[2, 3, 4, 5], [6]], [[7, 8, 9], [10, 11, 12], []]] */ ``` 第一個 **new** 創建了一個數組,這個數組首元素長度隨機,其余的則不確定。第二個 **new** 在 for 循環中給數組填充了第二個元素,第三個 **new** 為數組的最后一個索引填充元素。 * **[1]** Java 8 增加了 **Arrays.setAll()** 方法,其使用生成器來生成插入數組中的值。此生成器符合函數式接口 **IntUnaryOperator** ,只使用一個非 **默認** 的方法 **ApplyAsint(int操作數)** 。 **Arrays.setAll()** 傳遞當前數組索引作為操作數,因此一個選項是提供 **n -> n** 的 lambda 表達式來顯示數組的索引(在上面的代碼中很容易嘗試)。這里,我們忽略索引,只是插入遞增計數器的值。 非基元的對象數組也可以定義為不規則數組。這里,我們收集了許多使用大括號的 **new** 表達式: ```Java // arrays/MultidimensionalObjectArrays.java import java.util.*; public class MultidimensionalObjectArrays { public static void main(String[] args) { BerylliumSphere[][] spheres = { { new BerylliumSphere(), new BerylliumSphere() }, { new BerylliumSphere(), new BerylliumSphere(), new BerylliumSphere(), new BerylliumSphere() }, { new BerylliumSphere(), new BerylliumSphere(), new BerylliumSphere(), new BerylliumSphere(), new BerylliumSphere(), new BerylliumSphere(), new BerylliumSphere(), new BerylliumSphere() }, }; System.out.println(Arrays.deepToString(spheres)); } } /* Output: [[Sphere 0, Sphere 1], [Sphere 2, Sphere 3, Sphere 4, Sphere 5], [Sphere 6, Sphere 7, Sphere 8, Sphere 9, Sphere 10, Sphere 11, Sphere 12, Sphere 13]] */ ``` 數組初始化時使用自動裝箱技術: ```Java // arrays/AutoboxingArrays.java import java.util.*; public class AutoboxingArrays { public static void main(String[] args) { Integer[][] a = { // Autoboxing: { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 }, { 51, 52, 53, 54, 55, 56, 57, 58, 59, 60 }, { 71, 72, 73, 74, 75, 76, 77, 78, 79, 80 }, }; System.out.println(Arrays.deepToString(a)); } } /* Output: [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [21, 22, 23, 24, 25, 26, 27, 28, 29, 30], [51, 52, 53, 54, 55, 56, 57, 58, 59, 60], [71, 72, 73, 74, 75, 76, 77, 78, 79, 80]] */ ``` 以下是如何逐個構建非基元的對象數組: ```Java // arrays/AssemblingMultidimensionalArrays.java // Creating multidimensional arrays import java.util.*; public class AssemblingMultidimensionalArrays { public static void main(String[] args) { Integer[][] a; a = new Integer[3][]; for(int i = 0; i < a.length; i++) { a[i] = new Integer[3]; for(int j = 0; j < a[i].length; j++) a[i][j] = i * j; // Autoboxing } System.out.println(Arrays.deepToString(a)); } } /* Output: [[0, 0, 0], [0, 1, 2], [0, 2, 4]] */ ``` **i * j** 在這里只是為了向 **Integer** 中添加有趣的值。 **Arrays.deepToString()** 方法同時適用于基元數組和對象數組: ```JAVA // arrays/MultiDimWrapperArray.java // (c)2017 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. // Multidimensional arrays of "wrapper" objects import java.util.*; public class MultiDimWrapperArray { public static void main(String[] args) { Integer[][] a1 = { // Autoboxing { 1, 2, 3, }, { 4, 5, 6, }, }; Double[][][] a2 = { // Autoboxing { { 1.1, 2.2 }, { 3.3, 4.4 } }, { { 5.5, 6.6 }, { 7.7, 8.8 } }, { { 9.9, 1.2 }, { 2.3, 3.4 } }, }; String[][] a3 = { { "The", "Quick", "Sly", "Fox" }, { "Jumped", "Over" }, { "The", "Lazy", "Brown", "Dog", "&", "friend" }, }; System.out.println( "a1: " + Arrays.deepToString(a1)); System.out.println( "a2: " + Arrays.deepToString(a2)); System.out.println( "a3: " + Arrays.deepToString(a3)); } } /* Output: a1: [[1, 2, 3], [4, 5, 6]] a2: [[[1.1, 2.2], [3.3, 4.4]], [[5.5, 6.6], [7.7, 8.8]], [[9.9, 1.2], [2.3, 3.4]]] a3: [[The, Quick, Sly, Fox], [Jumped, Over], [The, Lazy, Brown, Dog, &, friend]] */ ``` 同樣的,在 **Integer** 和 **Double** 數組中,自動裝箱可為你創建包裝器對象。
                  <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>

                              哎呀哎呀视频在线观看