小编Min*_*Cho的帖子

Java中复制数组的不同方法

我正在通过我的学校提供​​的练习来解决这个问题。好吧,我几周前毕业了,在跳上 leetcode 之前正在浏览这个网站。

无论如何,这种方法swapAll使作为参数传递的两个数组相互复制。

对于那些无法查看问题的人,

编写一个名为的方法swapAll,它接受两个整数数组作为参数并交换它们的全部内容。您可以假设传递的数组不为空且长度相同。

例如,如果传递以下数组:

int[] a1 = {11, 42, -5, 27, 0, 89};
int[] a2 = {10, 20, 30, 40, 50, 60};
swapAll(a1, a2);

After the call, the arrays should store the following elements:

```java
a1: {10, 20, 30, 40, 50, 60}
a2: {11, 42, -5, 27, 0, 89}
Run Code Online (Sandbox Code Playgroud)

我实际上找到了两个解决此问题的方法

public static void swapAll(int[] a, int[] b) {
    int[] c = new int[a.length];
    for (int i = 0; i < a.length; …
Run Code Online (Sandbox Code Playgroud)

java arrays

-1
推荐指数
1
解决办法
49
查看次数

标签 统计

arrays ×1

java ×1