发帖    主题    评论    推荐    标签    作者    订阅    查搜    注册   登陆   关注
 
面向对象 设计模式 领域驱动设计 企业架构 框架 开发教程 微服务 CQRS 扩展性 并发编程 事件溯源 分布式 SOA

基础:如何有效的拷贝和合并数组

2002-12-03 15:18
赞助商链接

数组操作快,很多人喜欢用,但数组是定长的,难以动态扩展,一般用合并数组的办法。使用clone比较好实现数组拷贝:

英文:

It is ok to use clone() for arrays. In the case of arrays that are not of
primitive types, It does a "shallow:" copy meaning it doesn't copy the
objects that are referenced in the array. This is important in the case of
multi-dimensioned arrays. Because they also are just arrays of objects, the
sub-arrays don't get copied.

Use clone() only when you want to create an exact copy of the array, i.e.
same size, same element type.

System.arraycopy works on two existing arrays, so if you use that, you have
to create an empty array first. You would use it if you wanted to copy the
contents of an array to a bigger array (thereby "growing" the array) or if
you want to create an array where the element type is assignment-compatible
with the original array (e.g. going from String[] to Object[]). You can also
use it to move array elements within an array.

nmg
2002-12-03 15:55

可以用ArrayList 呀,
java.lang.Object
|
+-java.util.AbstractCollection
|
+-java.util.AbstractList
|
+-java.util.ArrayList

2002-12-04 15:24

不知道arrayList和array之间的转换在性能要求极高系统中是否耗时?

我觉得array定为原始元素,是java的一个败笔,据说Jdk1.5会改变。

赞助商链接

赞助商链接

返回顶部

移动版 关于本站 使用帮助 联系反馈 最佳分辨率1366x768
OpenSource JIVEJDON Powered by JdonFramework Code © 2002-20 jdon.com