1. 程式人生 > >JavaSE--Arrays.copyof

JavaSE--Arrays.copyof

背景:

想偷懶一次陣列賦值下面多個例子複製下陣列就好了..

以為 Arrays.copyof(Arrays.copyof內部呼叫的是 System.copy, 所以同 Arrays.copy)拷貝出來的陣列和原來的陣列是獨立不干擾的.

然後就悲劇了..

 

原來copy之後的陣列指向原陣列的地址.

例:https://blog.csdn.net/qq_27093465/article/details/54970538

 

 

 /**
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 by <code>src</code> to the destination array * referenced by <code>dest</code>. The number of components copied is * equal to the <code>length</code> argument. The components at
從 源陣列src 中複製子序列到 目標陣列dest, 子序列的長度由 length引數控制.
* positions <code>srcPos</code> through * <code>srcPos+length-1</code> in the source array are copied into * positions <code>destPos</code> through * <code>destPos+length-1</code>, respectively, of the destination * array.
從 src 陣列中的 srcPos, srcPos+length-1 的元素將會被複制到 dest 陣列的 destPos, destPos+length-1 位置. * <p> * If the <code>src</code> and <code>dest</code> arguments refer to the * same array object, then the copying is performed as if the * components at positions <code>srcPos</code> through * <code>srcPos+length-1</code> were first copied to a temporary * array with <code>length</code> components and then the contents of * the temporary array were copied into positions * <code>destPos</code> through <code>destPos+length-1</code> of the * destination array.
如果 src 和 dest 引用的是相同的陣列,那麼複製的過程就是首先從 陣列的 srcPos, srcPos+length-1 複製到 length 長度的臨時陣列,
然後在從臨時陣列中拷貝內容到這個陣列的 destPos, destPos+length-1 的位置. * <p> * If <code>dest</code> is <code>null</code>, then a * <code>NullPointerException</code> is thrown.
如果 dest 是 null, 會丟擲 NullPointerException 異常.
* <p> 
* If <code>src</code> is <code>null</code>, then a
* <code>NullPointerException</code> is thrown and the destination
* array is not modified.
如果 src 是 null, 也將會丟擲 NullPointException 異常,但是並不會改變 dest 的內容.
* <p>
* Otherwise, if any of the following is true, an
* <code>ArrayStoreException</code> is thrown and the destination is
* not modified:
另外,下面的情況都會丟擲 ArrayStoreException 異常,且都不會修改 dest 的內容.
* <ul>
* <li>The <code>src</code> argument refers to an object that is not an
* array.
  src 引用的物件不是陣列.
* <li>The <code>dest</code> argument refers to an object that is not an
* array.
  dest 引用的物件不是陣列.
* <li>The <code>src</code> argument and <code>dest</code> argument refer
* to arrays whose component types are different primitive types.
  src 和 dest 原始型別不同.
* <li>The <code>src</code> argument refers to an array with a primitive
* component type and the <code>dest</code> argument refers to an array
* with a reference component type.
  
* <li>The <code>src</code> argument refers to an array with a reference
* component type and the <code>dest</code> argument refers to an array
* with a primitive component type.
另外,下面的情況都會丟擲 IndexOutOfBoundsException 異常,且都不會修改 dest 的內容.
* </ul> 
* <p>
* Otherwise, if any of the following is true, an
* <code>IndexOutOfBoundsException</code> is
* thrown and the destination is not modified:
* <ul>
* <li>The <code>srcPos</code> argument is negative.
  srcPos 是負數.
* <li>The <code>destPos</code> argument is negative.
  destPos 是負數.
* <li>The <code>length</code> argument is negative.
  length 是負數.
* <li><code>srcPos+length</code> is greater than
  srcPos+length 大於 src 陣列的出長度
* <code>src.length</code>, the length of the source array.
* <li><code>destPos+length</code> is greater than
* <code>dest.length</code>, the length of the destination array.
  destPos+length 大於 dest 陣列的長度
* </ul>
* <p>
* Otherwise, if any actual component of the source array from
* position <code>srcPos</code> through
* <code>srcPos+length-1</code> cannot be converted to the component
* type of the destination array by assignment conversion, an
* <code>ArrayStoreException</code> is thrown. In this case, let
另外,如果 src 的元素不能轉換成 dest 目標的型別,也會丟擲 ArrayStoreException 異常.
* <b><i>k</i></b> be the smallest nonnegative integer less than
* length such that <code>src[srcPos+</code><i>k</i><code>]</code>
* cannot be converted to the component type of the destination
* array; when the exception is thrown, source array components from
* positions <code>srcPos</code> through
* <code>srcPos+</code><i>k</i><code>-1</code>
* will already have been copied to destination array positions
* <code>destPos</code> through
* <code>destPos+</code><i>k</I><code>-1</code> 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 <code>src</code>
* array could not be stored into the <code>dest</code> array
* because of a type mismatch.
*
@exception NullPointerException if either <code>src</code> or
* <code>dest</code> is <code>null</code>.

*/ 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         @Override
53         public String toString() {
54             return "User{" +
55                     "name='" + name + '\'' +
56                     '}';
57         }
58     }
59 
60 }