背景:
想偷懒一次数组赋值下面多个例子复制下数组就好了..
以为 Arrays.copyof(Arrays.copyof内部调用的是 System.copy, 所以同 Arrays.copy)拷贝出来的数组和原来的数组是独立不干扰的.
然后就悲剧了..
原来copy之后的数组指向原数组的地址.
例:
/**
arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
* Copies an array from the specified source array, beginning at the * specified position, to the specified position of the destination array. 从指定的 源数组位置 复制到 指定的目标数组的位置 来复制源数组. * A subsequence of array components are copied from the source * array referenced bysrc
to the destination array * referenced bydest
. The number of components copied is * equal to thelength
argument. The components at 从 源数组src 中复制子序列到 目标数组dest, 子序列的长度由 length参数控制. * positionssrcPos
through *srcPos+length-1
in the source array are copied into * positionsdestPos
through *destPos+length-1
, respectively, of the destination * array. 从 src 数组中的 srcPos, srcPos+length-1 的元素将会被复制到 dest 数组的 destPos, destPos+length-1 位置. ** If the
src
anddest
arguments refer to the * same array object, then the copying is performed as if the * components at positionssrcPos
through *srcPos+length-1
were first copied to a temporary * array withlength
components and then the contents of * the temporary array were copied into positions *destPos
throughdestPos+length-1
of the * destination array. 如果 src 和 dest 引用的是相同的数组,那么复制的过程就是首先从 数组的 srcPos, srcPos+length-1 复制到 length 长度的临时数组, 然后在从临时数组中拷贝内容到这个数组的 destPos, destPos+length-1 的位置. ** If
dest
isnull
, then a *NullPointerException
is thrown. 如果 dest 是 null, 会抛出 NullPointerException 异常.
** If
src
isnull
, then a *NullPointerException
is thrown and the destination * array is not modified. 如果 src 是 null, 也将会抛出 NullPointException 异常,但是并不会改变 dest 的内容. ** Otherwise, if any of the following is true, an *
ArrayStoreException
is thrown and the destination is * not modified: 另外,下面的情况都会抛出 ArrayStoreException 异常,且都不会修改 dest 的内容. *
- *
- The
src
argument refers to an object that is not an * array. src 引用的对象不是数组. * - The
dest
argument refers to an object that is not an * array. dest 引用的对象不是数组. * - The
src
argument anddest
argument refer * to arrays whose component types are different primitive types. src 和 dest 原始类型不同. * - The
src
argument refers to an array with a primitive * component type and thedest
argument refers to an array * with a reference component type. * - The
src
argument refers to an array with a reference * component type and thedest
argument refers to an array * with a primitive component type.
另外,下面的情况都会抛出 IndexOutOfBoundsException 异常,且都不会修改 dest 的内容.
* ** Otherwise, if any of the following is true, an *
IndexOutOfBoundsException
is * thrown and the destination is not modified: *
- *
- The
srcPos
argument is negative. srcPos 是负数. * - The
destPos
argument is negative. destPos 是负数. * - The
length
argument is negative. length 是负数. * srcPos+length
is greater than srcPos+length 大于 src 数组的出长度 *src.length
, the length of the source array. *destPos+length
is greater than *dest.length
, the length of the destination array. destPos+length 大于 dest 数组的长度 *
* Otherwise, if any actual component of the source array from * position srcPos
through * srcPos+length-1
cannot be converted to the component * type of the destination array by assignment conversion, an * ArrayStoreException
is thrown. In this case, let 另外,如果 src 的元素不能转换成 dest 目标的类型,也会抛出 ArrayStoreException 异常. * k be the smallest nonnegative integer less than * length such that src[srcPos+
k]
* cannot be converted to the component type of the destination * array; when the exception is thrown, source array components from * positions srcPos
through * srcPos+
k-1
* will already have been copied to destination array positions * destPos
through * destPos+
k-1
and no other * positions of the destination array will have been modified. * (Because of the restrictions already itemized, this * paragraph effectively applies only to the situation where both * arrays have component types that are reference types.) 特奶奶的,天书也没说到点子上,白看了.... * * @param src the source array. * @param srcPos starting position in the source array. * @param dest the destination array. * @param destPos starting position in the destination data. * @param length the number of array elements to be copied. * @exception IndexOutOfBoundsException if copying would cause * access of data outside array bounds. * @exception ArrayStoreException if an element in the src
* array could not be stored into the dest
array * because of a type mismatch. * @exception NullPointerException if either src
or * dest
is null
. */ public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
1 import java.util.Arrays; 2 3 public class ArrayCopyThink { 4 5 public static void main(String[] args) { 6 7 User u1 = new User("test1"); 8 User u2 = new User("test2"); 9 User u3 = new User("test3");10 User u4 = new User("test4");11 User[] users = {u1, u2, u3, u4};12 13 User u5 = new User("ooo");14 System.out.println("源数组 users: " + Arrays.toString(users));15 16 User[] copyUsers = new User[5];17 copyUsers[0] = u5;18 System.out.println("目标数组 copyUsers: " + Arrays.toString(copyUsers));19 20 System.out.println("将源数组的所有元素拷贝到目标数组的后面..");21 System.arraycopy(users, 0, copyUsers, 1, users.length);22 23 System.out.println("源数组 users: " + Arrays.toString(users));24 System.out.println("目标数组 copyUsers: " + Arrays.toString(copyUsers));25 26 System.out.println("更改拷贝数组拷贝过来的第一个元素,");27 copyUsers[1].setName("###");28 System.out.println("源数组 users: " + Arrays.toString(users));29 System.out.println("目标数组 copyUsers: " + Arrays.toString(copyUsers));30 31 32 33 34 }35 36 static class User {37 38 private String name;39 40 public User(String name) {41 this.name = name;42 }43 44 public String getName() {45 return name;46 }47 48 public void setName(String name) {49 this.name = name;50 }51 52 @Override53 public String toString() {54 return "User{" +55 "name='" + name + '\'' +56 '}';57 }58 }59 60 }