【发布时间】:2015-09-24 08:01:15
【问题描述】:
有没有一种方法可以在循环遍历数组时复制它?
所以,我想在循环过程还没有完成的情况下,将刚刚添加到数组中的当前元素的副本发送到类中的另一个方法来处理它,然后返回到前面的循环中方法。
我假设,如果可能的话,它可能是这样的:
String[] a = new String[10];
for (loop) {
a[0] = "Hello";
anotherMethod(Arrays.copyOf(a, a.length));
}
// anotherMethod(a) will get a copy of the array showing the
// first index of a, which is `Hello`, and will do similarly
// every time the first loop calls it.
【问题讨论】:
-
为什么你认为它不可能?试了有没有报错?
-
是的,你可以这样做。
-
@Henry 它给了我一个没有元素的空副本
-
for (loop): 我希望这不是你的实际代码。 -
@NickJ 哈哈哈!我希望一样