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

banq 02-12-03

数组操作快,很多人喜欢用,但数组是定长的,难以动态扩展,一般用合并数组的办法。使用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

banq
2002-12-04 15:24

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

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