是否有任何理由preFER System.arraycopy()以上的clone()? [英] Is there any reason to prefer System.arraycopy() over clone()?

查看:126
本文介绍了是否有任何理由preFER System.arraycopy()以上的clone()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当复制整个数组,我常常看到有人写:

When copying an entire array, I've often seen people write:

int[] dest = new int[orig.length];
System.arraycopy(orig, 0, dest, 0, orig.length);

但在我看来,没有理由偏爱这种过度:

But it seems to me there is no reason to favor this over:

int[] dest = orig.clone();

他们都是浅拷贝呢。也许这些人只是不知道,克隆存在。那么,有什么理由不使用克隆

They're both shallow copies anyway. Probably these folks just don't realize that clone exists. So is there any reason not to use clone?

推荐答案

如果你的真正的microbenchmarking,那么也许,这取决于JVM你运行。但实际上,没有。

No. If you're really microbenchmarking, then maybe, depending on what JVM you're running. But in actuality, no.

这篇关于是否有任何理由preFER System.arraycopy()以上的clone()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆