有什么理由更喜欢 System.arraycopy() 而不是 clone() 吗? [英] Is there any reason to prefer System.arraycopy() over clone()?

查看:29
本文介绍了有什么理由更喜欢 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();

无论如何,它们都是浅拷贝.可能这些人只是没有意识到 clone 的存在.那么有什么理由不使用 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?

推荐答案

不.如果您真的进行微基准测试,那么也许,这取决于您使用的是什么 JVM跑步.但实际上,没有.

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

这篇关于有什么理由更喜欢 System.arraycopy() 而不是 clone() 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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