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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java 復制數組 > 原文: [https://www.programiz.com/java-programming/copy-arrays](https://www.programiz.com/java-programming/copy-arrays) #### 在本教程中,您將在示例的幫助下了解可用于在 Java 中復制數組(一維和二維)的不同方法。 在 Java 中,我們可以將一個數組復制到另一個數組中。 您可以使用多種技術來復制 Java 中的數組。 * * * ## 1.使用賦值運算符復制數組 讓我們舉個例子 ```java class Main { public static void main(String[] args) { int [] numbers = {1, 2, 3, 4, 5, 6}; int [] positiveNumbers = numbers; // copying arrays for (int number: positiveNumbers) { System.out.print(number + ", "); } } } ``` **輸出**: ```java 1, 2, 3, 4, 5, 6 ``` 在上面的示例中,我們使用賦值運算符(`=`)將名為`numbers`的數組復制到名為`positiveNumbers`的另一個數組。 此技術是最簡單的一種,它也可以正常工作。 但是,該技術存在問題。 如果我們更改一個數組的元素,則其他數組的相應元素也會更改。 例如, ```java class Main { public static void main(String[] args) { int [] numbers = {1, 2, 3, 4, 5, 6}; int [] positiveNumbers = numbers; // copying arrays // change value of first array numbers[0] = -1; // printing the second array for (int number: positiveNumbers) { System.out.print(number + ", "); } } } ``` **輸出**: ```java -1, 2, 3, 4, 5, 6 ``` 在這里,我們可以看到我們更改了`number`數組的一個值。 當我們打印`positiveNumbers`數組時,我們可以看到相同的值也已更改。 這是因為兩個數組都引用同一個數組對象。 這是因為復制淺。 要了解有關淺拷貝的更多信息,請訪問[淺拷貝](http://stackoverflow.com/questions/1175620/in-java-what-is-a-shallow-copy)。 現在,要在復制數組時創建新的數組對象,我們需要深層復制而不是淺層復制。 * * * ## 2.使用循環結構復制數組 讓我們舉個例子: ```java import java.util.Arrays; class Main { public static void main(String[] args) { int [] source = {1, 2, 3, 4, 5, 6}; int [] destination = new int[6]; // iterate and copy elements from source to destination for (int i = 0; i < source.length; ++i) { destination[i] = source[i]; } // converting array to string System.out.println(Arrays.toString(destination)); } } ``` **輸出**: ```java [1, 2, 3, 4, 5, 6] ``` 在上面的示例中,我們使用`for`循環迭代源數組的每個元素。 在每次迭代中,我們都將元素從`source`數組復制到`destination`數組。 在此,源數組和目標數組引用不同的對象(深復制)。 因此,如果一個數組的元素改變,則另一數組的相應元素不變。 注意聲明, ```java System.out.println(Arrays.toString(destination)); ``` 在這里,`toString()`方法用于將數組轉換為字符串。 要了解更多信息,請訪問[`toString()`方法(Java 官方文檔)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--)。 * * * ## 3.使用`arraycopy()`方法復制數組 在 Java 中,[系統類](https://docs.oracle.com/javase/8/docs/api/java/lang/System.html)包含一個名為`arraycopy()`的方法來復制數組。 與上述兩種方法相比,此方法是復制數組的更好方法。 `arraycopy()`方法允許您將源數組的指定部分復制到目標數組。 例如, ```java arraycopy(Object src, int srcPos,Object dest, int destPos, int length) ``` 這里, * `src` - 您要復制的源數組 * `srcPos` - 源數組中的起始位置(索引) * `dest` - 將要從源復制元素的目標數組 * `destPos` - 目標數組中的起始位置(索引) * `length` - 要復制的元素數 Let's take an example: ```java // To use Arrays.toString() method import java.util.Arrays; class Main { public static void main(String[] args) { int[] n1 = {2, 3, 12, 4, 12, -2}; int[] n3 = new int[5]; // Creating n2 array of having length of n1 array int[] n2 = new int[n1.length]; // copying entire n1 array to n2 System.arraycopy(n1, 0, n2, 0, n1.length); System.out.println("n2 = " + Arrays.toString(n2)); // copying elements from index 2 on n1 array // copying element to index 1 of n3 array // 2 elements will be copied System.arraycopy(n1, 2, n3, 1, 2); System.out.println("n3 = " + Arrays.toString(n3)); } } ``` **輸出**: ```java n2 = [2, 3, 12, 4, 12, -2] n3 = [0, 12, 4, 0, 0] ``` 在上面的示例中,我們使用了`arraycopy()`方法, * `System.arraycopy(n1, 0, n2, 0, n1.length)` - 將`n1`數組中的整個元素復制到`n2`數組中 * `System.arraycopy(n1, 2, n3, 1, 2)` - 從索引`2`開始的`n1`數組的 2 個元素被復制到從索引`1`開始的`n3`數組 如您所見,`int`類型數組的元素的默認初始值為 0。 * * * ## 4.使用`copyOfRange()`方法復制數組 我們還可以使用 [Java `Arrays`](https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html)類中定義的`copyOfRange()`方法來復制數組。 例如, ```java // To use toString() and copyOfRange() method import java.util.Arrays; class ArraysCopy { public static void main(String[] args) { int[] source = {2, 3, 12, 4, 12, -2}; // copying entire source array to destination int[] destination1 = Arrays.copyOfRange(source, 0, source.length); System.out.println("destination1 = " + Arrays.toString(destination1)); // copying from index 2 to 5 (5 is not included) int[] destination2 = Arrays.copyOfRange(source, 2, 5); System.out.println("destination2 = " + Arrays.toString(destination2)); } } ``` **輸出** ```java destination1 = [2, 3, 12, 4, 12, -2] destination2 = [12, 4, 12] ``` 在上面的示例中,請注意以下行: ```java int[] destination1 = Arrays.copyOfRange(source, 0, source.length); ``` 在這里,我們可以看到正在創建`destination1`數組,并將`source`數組同時復制到該數組。 在調用`copyOfRange()`方法之前,我們不會創建`destination1`數組。 要了解有關該方法的更多信息,請訪問 [Java copyOfRange](https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#copyOfRange-boolean:A-int-int-) 。 * * * ## 5.使用循環復制二維數組 類似于一維數組,我們還可以使用`for`循環復制二維數組。 例如, ```java import java.util.Arrays; class Main { public static void main(String[] args) { int[][] source = { {1, 2, 3, 4}, {5, 6}, {0, 2, 42, -4, 5} }; int[][] destination = new int[source.length][]; for (int i = 0; i < destination.length; ++i) { // allocating space for each row of destination array destination[i] = new int[source[i].length]; for (int j = 0; j < destination[i].length; ++j) { destination[i][j] = source[i][j]; } } // displaying destination array System.out.println(Arrays.deepToString(destination)); } } ``` **輸出**: ```java [[1, 2, 3, 4], [5, 6], [0, 2, 42, -4, 5]] ``` 在上面的程序中,請注意以下行: ```java System.out.println(Arrays.deepToString(destination); ``` 在此,`deepToString()`方法用于更好地表示二維數組。 要了解更多信息,請訪問 [Java `deepToString()`](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#deepToString(java.lang.Object[]))。 * * * ## 使用`arraycopy()`復制 2d 數組 為了使上面的代碼更簡單,我們可以將內部循環替換為`System.arraycopy()`,就像一維數組一樣。 例如, ```java import java.util.Arrays; class Main { public static void main(String[] args) { int[][] source = { {1, 2, 3, 4}, {5, 6}, {0, 2, 42, -4, 5} }; int[][] destination = new int[source.length][]; for (int i = 0; i < source.length; ++i) { // allocating space for each row of destination array destination[i] = new int[source[i].length]; System.arraycopy(source[i], 0, destination[i], 0, destination[i].length); } // displaying destination array System.out.println(Arrays.deepToString(destination)); } } ``` **輸出**: ```java [[1, 2, 3, 4], [5, 6], [0, 2, 42, -4, 5]] ``` 在這里,我們看到通過用`arraycopy()`方法替換內部的`for`循環,我們得到了相同的輸出。
                  <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>

                              哎呀哎呀视频在线观看