共計 578 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Java 中,可以使用 System.arraycopy()
方法將兩個數組進行拼接。
示例代碼如下:
public class Main {public static void main(String[] args) {
// 定義兩個數組
int[] array1 = {1, 2, 3};
int[] array2 = {4, 5, 6};
// 創建一個新數組,長度為兩個數組的長度之和
int[] result = new int[array1.length + array2.length];
// 將 array1 復制到 result 中
System.arraycopy(array1, 0, result, 0, array1.length);
// 將 array2 復制到 result 中
System.arraycopy(array2, 0, result, array1.length, array2.length);
// 輸出結果
for (int i : result) {System.out.print(i + " ");
}
}
}
運行上述代碼,輸出結果為:1 2 3 4 5 6
。
在代碼中,我們首先定義了兩個數組 array1
和array2
,然后創建了一個新數組 result
,長度為兩個數組長度之和。接下來,使用System.arraycopy()
方法將 array1
和array2
分別復制到 result
數組中,然后通過循環輸出結果。
丸趣 TV 網 – 提供最優質的資源集合!
正文完