ArrayList浅拷贝迭代或克隆() [英] ArrayList shallow copy iterate or clone()

查看:284
本文介绍了ArrayList浅拷贝迭代或克隆()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个java ArrayList 的浅表副本,我应该使用 clone()还是迭代原始列表并将元素复制到新的arrayList中,哪个更快?

I need a shallow copy of an java ArrayList, should I use clone() or iterate over original list and copy elements in to new arrayList, which is faster ?

推荐答案

使用 clone(),或者使用copy-constructor。

Use clone(), or use the copy-constructor.

复制构造函数从传递的集合到数组进行额外的转换,而克隆()方法直接使用内部数组。

The copy-constructor makes additional transformation from the passed collection to array, while the clone() method uses the internal array directly.

请记住 clone()返回对象,因此您必须转换为列表

Have in mind that clone() returns Object, so you will have to cast to List.

这篇关于ArrayList浅拷贝迭代或克隆()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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