背景:

想偷懒一次数组赋值下面多个例子复制下数组就好了..

以为 Arrays.copyof(Arrays.copyof内部调用的是 System.copy, 所以同 Arrays.copy)拷贝出来的数组和原来的数组是独立不干扰的.

然后就悲剧了..

原来copy之后的数组指向原数组的地址.

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

  1. /**
  1. arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
  1. * Copies an array from the specified source array, beginning at the
    * specified position, to the specified position of the destination array.
    从指定的 源数组位置 复制到 指定的目标数组的位置 来复制源数组.
  2. * A subsequence of array components are copied from the source
  3. * array referenced by <code>src</code> to the destination array
  4. * referenced by <code>dest</code>. The number of components copied is
  5. * equal to the <code>length</code> argument. The components at
    源数组src 中复制子序列到 目标数组dest, 子序列的长度由 length参数控制.
  6. * positions <code>srcPos</code> through
  7. * <code>srcPos+length-1</code> in the source array are copied into
  8. * positions <code>destPos</code> through
  9. * <code>destPos+length-1</code>, respectively, of the destination
  10. * array.
    src 数组中的 srcPos, srcPos+length-1 的元素将会被复制到 dest 数组的 destPos, destPos+length-1 位置.
  11. * <p>
  12. * If the <code>src</code> and <code>dest</code> arguments refer to the
  13. * same array object, then the copying is performed as if the
  14. * components at positions <code>srcPos</code> through
  15. * <code>srcPos+length-1</code> were first copied to a temporary
  16. * array with <code>length</code> components and then the contents of
  17. * the temporary array were copied into positions
  18. * <code>destPos</code> through <code>destPos+length-1</code> of the
  19. * destination array.
    如果 src dest 引用的是相同的数组,那么复制的过程就是首先从 数组的 srcPos, srcPos+length-1 复制到 length 长度的临时数组,
    然后在从临时数组中拷贝内容到这个数组的 destPos, destPos+length-1 的位置.
  20. * <p>
  21. * If <code>dest</code> is <code>null</code>, then a
  22. * <code>NullPointerException</code> is thrown.
    如果 dest null, 会抛出 NullPointerException 异常.
  1. * <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.
  1. 另外,下面的情况都会抛出 IndexOutOfBoundsException 异常,且都不会修改 dest 的内容.
  1. * </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>.
  2.  
  3. */ 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. static class User {
  34.  
  35. private String name;
  36.  
  37. public User(String name) {
  38. this.name = name;
  39. }
  40.  
  41. public String getName() {
  42. return name;
  43. }
  44.  
  45. public void setName(String name) {
  46. this.name = name;
  47. }
  48.  
  49. @Override
  50. public String toString() {
  51. return "User{" +
  52. "name='" + name + '\'' +
  53. '}';
  54. }
  55. }
  56.  
  57. }

JavaSE--Arrays.copyof的更多相关文章

  1. System.arraycopy()和Arrays.copyOf()的区别

    先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, in ...

  2. Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别

    如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 1.示例代码: Sys ...

  3. java数组的拷贝四种方法:for、clone、System.arraycopy、arrays.copyof

    public class ArrayCopy{ public static void main(String []args){ int []a = {1,3,4,5}; toPrint(a); int ...

  4. Arrays.copyof

    public static int[] copyOf(int[] original, int newLength) { int[] copy = new int[newLength]; System. ...

  5. System.arraycopy(src, srcPos, dest, destPos, length) 与 Arrays.copyOf(original, newLength)区别

    //System.arraycopy,只拷贝已存在的数组元素 int[] src = {0, 1, 2}; int[] dest = new int[3]; System.arraycopy(src, ...

  6. 数组复制的五种方式(遍历循环一一赋值、System.arraycopy、地址赋值、克隆clone()、Arrays.copyof())

    package com.Summer_0424.cn; import java.util.Arrays; import java.util.concurrent.CopyOnWriteArrayLis ...

  7. Arrays.copyOf() 和 System.arrayCopy()分析

    java数组的拷贝四种方法:for.clone.System.arraycopy.Arrays.copyof public class Test1 { public static void main( ...

  8. Java中 System.arraycopy() 和 Arrays.copyOf()方法

    System.arraycopy() 和 Arrays.copyOf()方法 阅读源码的话,我们就会发现 ArrayList 中大量调用了这两个方法.比如:我们上面讲的扩容操作以及add(int in ...

  9. System.arraycopy和arrays.copyOf

    public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); 这 ...

  10. java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别

    java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别 一.java.lang.System.arraycopy() 该方法的声明: /* ...

随机推荐

  1. RTL8711AM

    官方文档主要修改 为了实现log服务 1,在log_service.h 取消注释 #ifndef LOG_SERVICE_H #define LOG_SERVICE_H #include " ...

  2. Swift 语法糖then

    then是一个swift初始化库,只有80几行的代码库,确可以让初始化变得很优雅. 1.使用then初始化AnyObject,这里以初始化控件为例 lazy var label = UILabel() ...

  3. JAVA基础-反射机制

    什么是JAVA的反射机制 Java反射是Java被视为动态(或准动态)语言的一个关键性质.这个机制允许程序在运行时透过Reflection APIs取得任何一个已知名称的class的内部信息,包括其 ...

  4. C++中的简单继承

    Father.cpp: #include<iostream> using namespace std; class Father { protected: int width; int h ...

  5. UML图表示类之间的关系

    一.泛化(Generanization) 图: 泛化简单的说就是继承关系,在java中就是extend.表示一般与特殊的关系.如鸭子是鸟的一种,即有鸭子的特性也有鸟的共性.用带空心的三角箭头的实线指向 ...

  6. django中使用ORM模型修改数据库的表名

    在django中,使用models.py创建好一张表后,如果不指定表的名字,那么表的名字就默认为 model_modelname 例如: class Book(models.Model): id = ...

  7. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-backward

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  8. 基于Ambari的WebUI实现集群扩容案例

    基于Ambari的WebUI实现集群扩容案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.将HDP的服务托管给Ambari服务 1>.点击“Service Auto S ...

  9. JavaScript中querySelector()和getElementById()(getXXXByXX)的区别

    在日常开发中,使用JavaScript获取元素的时候,最常用的方法就是document.getElementById(getXXXByXX)方法.但是最近发现有很多地方使用的是querySelecto ...

  10. SpringBoot#自定义配置的封装

    _震惊,开局 不可避免的需要弄一些自定义的配置. 要点: 1. 把配置项都写出来,分析层次关系:2. 抽象成bean与bean之间的关系,写出bean对应的类,这时候配置项对应了bean的属性,属性可 ...