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);
import java.util.Arrays;
public class ArrayCopyThink {
public static void main(String[] args) {
User u1 = new User("test1");
User u2 = new User("test2");
User u3 = new User("test3");
User u4 = new User("test4");
User[] users = {u1, u2, u3, u4};
User u5 = new User("ooo");
System.out.println("源数组 users: " + Arrays.toString(users));
User[] copyUsers = new User[5];
copyUsers[0] = u5;
System.out.println("目标数组 copyUsers: " + Arrays.toString(copyUsers));
System.out.println("将源数组的所有元素拷贝到目标数组的后面..");
System.arraycopy(users, 0, copyUsers, 1, users.length);
System.out.println("源数组 users: " + Arrays.toString(users));
System.out.println("目标数组 copyUsers: " + Arrays.toString(copyUsers));
System.out.println("更改拷贝数组拷贝过来的第一个元素,");
copyUsers[1].setName("###");
System.out.println("源数组 users: " + Arrays.toString(users));
System.out.println("目标数组 copyUsers: " + Arrays.toString(copyUsers));
}
static class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
}

JavaSE--Arrays.copyof的更多相关文章
- System.arraycopy()和Arrays.copyOf()的区别
先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, in ...
- Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别
如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 1.示例代码: Sys ...
- 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 ...
- Arrays.copyof
public static int[] copyOf(int[] original, int newLength) { int[] copy = new int[newLength]; System. ...
- 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, ...
- 数组复制的五种方式(遍历循环一一赋值、System.arraycopy、地址赋值、克隆clone()、Arrays.copyof())
package com.Summer_0424.cn; import java.util.Arrays; import java.util.concurrent.CopyOnWriteArrayLis ...
- Arrays.copyOf() 和 System.arrayCopy()分析
java数组的拷贝四种方法:for.clone.System.arraycopy.Arrays.copyof public class Test1 { public static void main( ...
- Java中 System.arraycopy() 和 Arrays.copyOf()方法
System.arraycopy() 和 Arrays.copyOf()方法 阅读源码的话,我们就会发现 ArrayList 中大量调用了这两个方法.比如:我们上面讲的扩容操作以及add(int in ...
- System.arraycopy和arrays.copyOf
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); 这 ...
- java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别
java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别 一.java.lang.System.arraycopy() 该方法的声明: /* ...
随机推荐
- 论文阅读:Blink-Fast Connectivity Recovery Entirely in the Data Plane
1.背景 在网络中,链路故障的发生在所难免,为了降低故障带来的影响,就需要重新路由,将数据传输到合适的链路上.当因为链路故障发生处的不同,也有不同的解决方法. AS(Autonomous System ...
- Java笔记--集合
1.Java集合类可以用于存储数量不等的多个对象,还可以用于保存具有映射关系的关联数组. 2.Java集合可分为Collection和Map两种体系: --Collection:1)Set:元素无序. ...
- 微信小程序—添加背景音乐
问题: 想在打开小程序时就自动播放背景音乐(循环) 解决方法: 1.思路:写一个函数,在 onLoad()中调用 2. //index.js //获取应用实例 const back = wx.get ...
- 006.Oracle数据库 , DISTINCT去掉重复项重复内容
/*Oracle数据库查询日期在两者之间*/ SELECT DISTINCT OCCUR_DATE FROM LM_FAULT WHERE ( ( OCCUR_DATE >= to_date( ...
- IDE及PHP基础——注释、变量、数据、运算符、输出等
IDE(Integrated Development Environment ),集成开发环境,是用于提供程序开发环境的应用程序,一般包括代码编辑器.编译器.调试器和图形用户界面等工具.集成了代码编写 ...
- SpringBoot-配置Java方式
SpringBoot中使用Java方式配置步骤如下: 在类上加入@Configuration注解,代表作为配置类 在该类方法上加入@Bean注解,代表将方法返回的Bean加入Spring容器 在该类中 ...
- 解决d7在更高版本上运行乱码问题,或者是调用更高版本的dll
将String类型改成WideString类型即可
- WC2020 联训 #19 矩阵
好不容易自己切一道题 链接 Description 在一个 \(n×(n+1)\) 的棋盘上放棋子, \(n\) 行中每行都恰好有两枚棋子,并且 \(n+1\) 列中每列都至多有两枚棋子,设 \(n= ...
- IDEA快速定位一个文件到项目目录
第一步:快捷键搜索java文件关键字 快捷键Ctrl+N,如果设置为Eclipse版本快捷键为Ctrl+Shift+R 第二步:定位文件到项目目录中 1.在当前文件下 2.点击定位按钮 3.定位到项目 ...
- UVA - 1152 4 Values whose Sum is 0(中途相遇法)
题意:从四个集合各选一个数,使和等于0,问有多少种选法. 分析:求出来所有ai + bi,在里面找所有等于ci + di的个数. #pragma comment(linker, "/STAC ...